#Okay
1 messages · Page 1 of 1 (latest)
So look, I'm making a switch for different SDKs
in one SDK, settings are stored in a scriptable object
in another in a regular CS file
when switching from the first SDK to the second, I would like to replace the values of some fields directly in the CS file with those specified in the CS file
'directly in the CS file with those specified in the CS file' ?
public class SecondSdkConfig
{
public const string APP_KEY_ANDROID = "qweqwe";
public const string APP_SECRET_ANDROID = "dkfslfgdsfkmasdfjlasdcs";
public const string PROMO_KEY_IOS = "";
/* App constants */
public const string AD_INTERSTITIAL = "ad_interstitial";
public const string AD_REWARDED = "ad_rewarded";
}
and after clicking on the button in the editor, I want the values from the config to be pulled directly into the file and it will look like this
public class SecondSdkConfig
{
public const string APP_KEY_ANDROID = "my_test_app_key_android";
public const string APP_SECRET_ANDROID = "my_test_app_secret_android";
public const string PROMO_KEY_IOS = "new_PROMO_KEY";
/* App constants */
public const string AD_INTERSTITIAL = "new_ad_interstitial";
public const string AD_REWARDED = "new_ad_rewarded";
}
I am not following you. The first class is what you start with and the second is how you want it to end up?
Yes. I have a source file, I would like to take it and replace the field values with those specified in the scriptable object of another plugin
ok. So first I would make a template script something like
public partial class SecondSdkConfig
{
public const string APP_KEY_ANDROID = "$KEY$";
public const string APP_SECRET_ANDROID = "$SECRET$";
public const string PROMO_KEY_IOS = "$PROMO$";
/* App constants */
public const string AD_INTERSTITIAL = "$INTERSTITIAL$";
public const string AD_REWARDED = "$REWARDED$";
}
then it's just a question of reading that file and doing string substitution
also make sure it's in a namespace
also make the class partial because then it's easy for the user to extend it if necessary
so you end up with a method that looks something like
string[] linesIn = File.ReadAllLines(template.cs);
string[] linesOut = new string[linesIn.Length];
for (int i=0;i<linesIn.Length;i++) {
string line = linesIn[i];
if (line.Contains("$KEY$") line = line.Replace("$KEY$",field from SO);
... etc etc
linesOut[i] = line;
}
File.WriteAllLines("newcs.cs", linesOut);
OK. I understand this method, thank you.
There is another question. I also have a specific config (a regular serializable class). There is also a completely empty class. I would like to rewrite fields from one class to another class with the current serialized values.
[Serializable]
public class GameConfig
{
}
[Serializable]
public class GameConfig
{
public string active_zoom = "1";
public int version = 1;
public int big_level_challenge_type = 0;
public int first_challenge_is_free = 1;
public int environment_props_version = 0;
}
I want to generate this field conpletely
Do something like template strings and also generate for each field a string like “public/private type = value”?
I can generate each field like this. But then how can I get the current serializable values of the fields of my second class in order to
transfer them to the generated one?
for that you will need to use Reflection.
You can get all of the FieldInfo data for the class then you loop through this and use GetValue to get the current value from an instance of the class, with those you should be able to build the new class