Quantcast

Jump to content

» «
Photo

save var

17 replies to this topic
Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#1

Posted 03 April 2014 - 04:40 PM

How do i save a variable that the player cannot edit?

julionib
  • julionib

    Coder

  • Feroci
  • Joined: 13 Sep 2012
  • Brazil

#2

Posted 03 April 2014 - 06:46 PM

you mean a data into .ini file or something like that?


Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#3

Posted 03 April 2014 - 07:45 PM

you mean a data into .ini file or something like that?

 

Hmm no, just like money. It is stored somewhere, but the player can't edit it, only by using code


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#4

Posted 03 April 2014 - 07:49 PM Edited by LordOfTheBongs, 03 April 2014 - 08:06 PM.

use encryption

namespace EncryptionExample
{
    using System;
    using System.Text;
    using System.Security.Cryptography;
 
    internal class Security
    {
        private static byte[] entropy = Encoding.Unicode.GetBytes("LordOfTheBongs");//this is used in the algorithm for encryption and you can make any random string value u like... u should read about entropy online for what this is for
 
        internal static string EncryptString(string input)
        {
            byte[] encryptedData = ProtectedData.Protect(
                Encoding.Unicode.GetBytes(input),
                entropy,
                DataProtectionScope.CurrentUser);
            return Convert.ToBase64String(encryptedData);
        }
 
        internal static string DecryptString(string encryptedData)
        {
            try
            {
                byte[] decryptedData = ProtectedData.Unprotect(
                    Convert.FromBase64String(encryptedData),
                    entropy,
                    DataProtectionScope.CurrentUser);
                return Encoding.Unicode.GetString(decryptedData);
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}

example of use...

int someNumber = 12;//u wanna save this but not let player edit the dat file
 
//first convert to string
string saveValue = someNumber.ToString();
 
//i'll assume u use streamwriter class to write to your dat file and u put a setting on it's own line
streamWriter.WriteLine(Security.EncryptString(saveValue));
 
//then to load ill assume u use streamreader class and the reader is in position to read the line with encrypted string
int someNumber = int.Parse(Security.DecryptString(reader.ReadLine()));

you could also write the byte value of the string and would look crazy in a text file but that is easier to "hack" i guess


Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#5

Posted 03 April 2014 - 08:13 PM Edited by Wiebrendh, 03 April 2014 - 08:14 PM.

use encryption

namespace EncryptionExample
{
    using System;
    using System.Text;
    using System.Security.Cryptography;
 
    internal class Security
    {
        private static byte[] entropy = Encoding.Unicode.GetBytes("LordOfTheBongs");//this is used in the algorithm for encryption and you can make any random string value u like... u should read about entropy online for what this is for
 
        internal static string EncryptString(string input)
        {
            byte[] encryptedData = ProtectedData.Protect(
                Encoding.Unicode.GetBytes(input),
                entropy,
                DataProtectionScope.CurrentUser);
            return Convert.ToBase64String(encryptedData);
        }
 
        internal static string DecryptString(string encryptedData)
        {
            try
            {
                byte[] decryptedData = ProtectedData.Unprotect(
                    Convert.FromBase64String(encryptedData),
                    entropy,
                    DataProtectionScope.CurrentUser);
                return Encoding.Unicode.GetString(decryptedData);
            }
            catch
            {
                return string.Empty;
            }
        }
    }
}

example of use...

int someNumber = 12;//u wanna save this but not let player edit the dat file
 
//first convert to string
string saveValue = someNumber.ToString();
 
//i'll assume u use streamwriter class to write to your dat file and u put a setting on it's own line
streamWriter.WriteLine(Security.EncryptString(saveValue));
 
//then to load ill assume u use streamreader class and the reader is in position to read the line with encrypted string
int someNumber = int.Parse(Security.DecryptString(reader.ReadLine()));

you could also write the byte value of the string and would look crazy in a text file but that is easier to "hack" i guess

 

That looks pretty difficult for a beginner, but gonna figure it out

 

And i have to idea what streamwriter is..


LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#6

Posted 03 April 2014 - 08:18 PM Edited by LordOfTheBongs, 03 April 2014 - 08:30 PM.

the Security class can be copied and pasted as is into your script's namespace. If you use visual studio add a class yo your project and paste all my code over the new class u added, make sure u change my example namespace name to your project's namespace then u can change any string to an encrypted value and u can read any value your script previously encrypted...

 

then it is simple to use as u can see in the 2nd code box. to encrypt and write to a dat file is 1 line of code... it looks like a lot on first glance but it really isnt ;)

 

just ask for help if u need

  • Wiebrendh likes this

pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#7

Posted 04 April 2014 - 10:51 AM Edited by pedro2555, 04 April 2014 - 10:52 AM.

For more complicated objects, you might want to take a look at serialization.

 

http://msdn.microsof...(v=vs.110).aspx


Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#8

Posted 04 April 2014 - 12:24 PM Edited by Wiebrendh, 04 April 2014 - 12:32 PM.

 .


Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#9

Posted 04 April 2014 - 12:31 PM

the Security class can be copied and pasted as is into your script's namespace. If you use visual studio add a class yo your project and paste all my code over the new class u added, make sure u change my example namespace name to your project's namespace then u can change any string to an encrypted value and u can read any value your script previously encrypted...

 

then it is simple to use as u can see in the 2nd code box. to encrypt and write to a dat file is 1 line of code... it looks like a lot on first glance but it really isnt ;)

 

just ask for help if u need

 

I made a new .cs file, added that script to it, edited the namespace, and what do i have to place in the "LordOfTheBongs" thing, and how do i get the encrypted data in the first class?


pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#10

Posted 04 April 2014 - 02:13 PM Edited by pedro2555, 04 April 2014 - 02:15 PM.

Here is a serialization example, I think it's a much more out of the box solution.

 

Here is a wrapper of the msdn example:

    static void Serialize(string fullFilePath, object data)
    {
        FileStream fs = new FileStream(fullFilePath, FileMode.Create);
        try
        {
            new BinaryFormatter().Serialize(fs, data);
        }
        catch (SerializationException e)
        {
            // Serialization failed
        }
        finally
        {
            fs.Close();
        }
    }

    static object Deserialize(string fullFilePath)
    {
        object result;
        FileStream fs = new FileStream(fullFilePath, FileMode.Open);
        try
        {
            result = new BinaryFormatter().Deserialize(fs);
        }
        catch (SerializationException e)
        {
            // Deserialization failed
        }
        finally
        {
            fs.Close();
        }
        return result;
    }


    // Usage

    string data = "4410d99cefe57ec2c2cdbd3f1d5cf862bb4fb6f8";
    // Save data
    Serialize(@"C:\data.dat", data);
    // Load data
    string dataBack = (string)Deserialize(@"C:\data.dat");

LordOfTheBongs
  • LordOfTheBongs

    Deceased

  • Members
  • Joined: 21 Sep 2013
  • None

#11

Posted 04 April 2014 - 02:24 PM

 

the Security class can be copied and pasted as is into your script's namespace. If you use visual studio add a class yo your project and paste all my code over the new class u added, make sure u change my example namespace name to your project's namespace then u can change any string to an encrypted value and u can read any value your script previously encrypted...

 

then it is simple to use as u can see in the 2nd code box. to encrypt and write to a dat file is 1 line of code... it looks like a lot on first glance but it really isnt ;)

 

just ask for help if u need

 

I made a new .cs file, added that script to it, edited the namespace, and what do i have to place in the "LordOfTheBongs" thing, and how do i get the encrypted data in the first class?

 

u can change to anything... it simply needs to always be the same string value... this entropy object is used in the math formula that calculates the encrypted string... along with your user credentials, it makes the common encryption algorithm that everyone uses from .net unique

 

u should read about what entropy is, google it ;)

 

the first code box is copy pasted onto your new class file and u changed the namespace to your project's namespace... so good. Then depending how u write to the file just know u need to use the encrypt method on the string that u write and then use decrypt on the encrypted string u read (2nd code box)


Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#12

Posted 04 April 2014 - 02:50 PM

 

Here is a serialization example, I think it's a much more out of the box solution.

 

Here is a wrapper of the msdn example:

    static void Serialize(string fullFilePath, object data)
    {
        FileStream fs = new FileStream(fullFilePath, FileMode.Create);
        try
        {
            new BinaryFormatter().Serialize(fs, data);
        }
        catch (SerializationException e)
        {
            // Serialization failed
        }
        finally
        {
            fs.Close();
        }
    }

    static object Deserialize(string fullFilePath)
    {
        object result;
        FileStream fs = new FileStream(fullFilePath, FileMode.Open);
        try
        {
            result = new BinaryFormatter().Deserialize(fs);
        }
        catch (SerializationException e)
        {
            // Deserialization failed
        }
        finally
        {
            fs.Close();
        }
        return result;
    }


    // Usage

    string data = "4410d99cefe57ec2c2cdbd3f1d5cf862bb4fb6f8";
    // Save data
    Serialize(@"C:\data.dat", data);
    // Load data
    string dataBack = (string)Deserialize(@"C:\data.dat");

nothing gives error, expect for the one of the last lines of code.. 

Serialize(@"C:\data.dat", data); 

this line gives 2 errors..

error one:

Invalid token ')' in class, struct, or interface member declaration

error two:

Method must have a return type


pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#13

Posted 04 April 2014 - 08:13 PM

Man, do you know anything about C# at all ?

 

Off course it trows those errors, that is just an example sample. You should place the lines bellow // Usage somewhere in your code flow, eg: where you need to save/load your data.

 

The rest is just a ready made methods.


Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#14

Posted 05 April 2014 - 06:15 AM

Man, do you know anything about C# at all ?

 

Off course it trows those errors, that is just an example sample. You should place the lines bellow // Usage somewhere in your code flow, eg: where you need to save/load your data.

 

The rest is just a ready made methods.

 

Yeah, i know a little bit about C# and that it :(


pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#15

Posted 05 April 2014 - 07:44 AM Edited by pedro2555, 05 April 2014 - 07:46 AM.

 

Man, do you know anything about C# at all ?

 

Off course it trows those errors, that is just an example sample. You should place the lines bellow // Usage somewhere in your code flow, eg: where you need to save/load your data.

 

The rest is just a ready made methods.

 

Yeah, i know a little bit about C# and that it :(

 

 

Well then, you should know what code flow is.

 

What I and LorfOfTheBongs provided was just 2 functions, and instructions on how to use them on your code flow. It isn't just copy and paste everything into a .cs file and everything will automagically work.

 

I suggest you to learn a bit more about the language:

 

http://msdn.microsof...studio/hh341490 -> For complete begginers

http://msdn.microsof...y/67ef8sbd.aspx -> Nice set of simple tutorials

 

http://channel9.msdn...olute-Beginners -> If you are American (no offense intended)

http://msdn.microsof...6(v=vs.71).aspx -> To get your head around some more stuff

 

http://gtaxscripting.../tutorials.html -> Alternatively, you can wrap up C#, learn VB.NET and then (and only then) follow JulioNIB's tutorials on GTA Scripting with VB.NET

 

With that done, I will assure you that you can come here, ask questions and understand the replies.


Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#16

Posted 05 April 2014 - 08:33 AM Edited by Wiebrendh, 05 April 2014 - 08:34 AM.

 

 

Man, do you know anything about C# at all ?

 

Off course it trows those errors, that is just an example sample. You should place the lines bellow // Usage somewhere in your code flow, eg: where you need to save/load your data.

 

The rest is just a ready made methods.

 

Yeah, i know a little bit about C# and that it :(

 

 

Well then, you should know what code flow is.

 

What I and LorfOfTheBongs provided was just 2 functions, and instructions on how to use them on your code flow. It isn't just copy and paste everything into a .cs file and everything will automagically work.

 

I suggest you to learn a bit more about the language:

 

http://msdn.microsof...studio/hh341490 -> For complete begginers

http://msdn.microsof...y/67ef8sbd.aspx -> Nice set of simple tutorials

 

http://channel9.msdn...olute-Beginners -> If you are American (no offense intended)

http://msdn.microsof...6(v=vs.71).aspx -> To get your head around some more stuff

 

http://gtaxscripting.../tutorials.html -> Alternatively, you can wrap up C#, learn VB.NET and then (and only then) follow JulioNIB's tutorials on GTA Scripting with VB.NET

 

With that done, I will assure you that you can come here, ask questions and understand the replies.

 

Oke, thanks, gonna watch it all, is this http://thenewboston....list.php?cat=15 also usefull to understand this?


pedro2555
  • pedro2555

    Open Sourcer

  • Members
  • Joined: 02 Sep 2012
  • Portugal

#17

Posted 05 April 2014 - 09:51 AM

 

 

 

Man, do you know anything about C# at all ?

 

Off course it trows those errors, that is just an example sample. You should place the lines bellow // Usage somewhere in your code flow, eg: where you need to save/load your data.

 

The rest is just a ready made methods.

 

Yeah, i know a little bit about C# and that it :(

 

 

Well then, you should know what code flow is.

 

What I and LorfOfTheBongs provided was just 2 functions, and instructions on how to use them on your code flow. It isn't just copy and paste everything into a .cs file and everything will automagically work.

 

I suggest you to learn a bit more about the language:

 

http://msdn.microsof...studio/hh341490 -> For complete begginers

http://msdn.microsof...y/67ef8sbd.aspx -> Nice set of simple tutorials

 

http://channel9.msdn...olute-Beginners -> If you are American (no offense intended)

http://msdn.microsof...6(v=vs.71).aspx -> To get your head around some more stuff

 

http://gtaxscripting.../tutorials.html -> Alternatively, you can wrap up C#, learn VB.NET and then (and only then) follow JulioNIB's tutorials on GTA Scripting with VB.NET

 

With that done, I will assure you that you can come here, ask questions and understand the replies.

 

Oke, thanks, gonna watch it all, is this http://thenewboston....list.php?cat=15 also usefull to understand this?

 

Quite comprehensive at first glance, so yes, useful. But be sure to understand the concepts used in the videos.


Wiebrendh
  • Wiebrendh

    C# Programmer

  • Members
  • Joined: 22 Mar 2014
  • Netherlands

#18

Posted 05 April 2014 - 09:57 AM

 

 

 

 

Man, do you know anything about C# at all ?

 

Off course it trows those errors, that is just an example sample. You should place the lines bellow // Usage somewhere in your code flow, eg: where you need to save/load your data.

 

The rest is just a ready made methods.

 

Yeah, i know a little bit about C# and that it :(

 

 

Well then, you should know what code flow is.

 

What I and LorfOfTheBongs provided was just 2 functions, and instructions on how to use them on your code flow. It isn't just copy and paste everything into a .cs file and everything will automagically work.

 

I suggest you to learn a bit more about the language:

 

http://msdn.microsof...studio/hh341490 -> For complete begginers

http://msdn.microsof...y/67ef8sbd.aspx -> Nice set of simple tutorials

 

http://channel9.msdn...olute-Beginners -> If you are American (no offense intended)

http://msdn.microsof...6(v=vs.71).aspx -> To get your head around some more stuff

 

http://gtaxscripting.../tutorials.html -> Alternatively, you can wrap up C#, learn VB.NET and then (and only then) follow JulioNIB's tutorials on GTA Scripting with VB.NET

 

With that done, I will assure you that you can come here, ask questions and understand the replies.

 

Oke, thanks, gonna watch it all, is this http://thenewboston....list.php?cat=15 also usefull to understand this?

 

Quite comprehensive at first glance, so yes, useful. But be sure to understand the concepts used in the videos.

 

Oke, nice that is a very good teacher





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users