0

I have Lua scripts, that uses variables such as:

VERSION_LOCALE = "1.0"
MAX_MONSTERS = 5
FORBIDDEN_MONSTERS = {2827}

I would like to make the variables externally configurable using a simple C# Program.

  1. Load the Lua script from a dialog
  2. Overwrite the file with the modified variables (textbox)
  3. The actual variables retrieved from our Lua script, in our example MAX_MONSTERS should be returned in the textbox.

Program Idea

What is the appropriate way to achieve? Here is what I have tried without success: https://stackoverflow.com/a/27219221/18756404

Nifim
  • 4,758
  • 2
  • 12
  • 31

2 Answers2

0

Here's what you need to do:

  1. Read the file contents using System.IO.File.ReadAllText()
  2. Split the lines in to an array using String.Split()
  3. Parse the value using String.SubString() and show the value in the text box
  4. Read the value from the text box and update the array
  5. Regenerate the new string using String.Join()
  6. Write the new string to the file using System.IO.File.WriteAllText()
Shameel
  • 632
  • 5
  • 12
0

You are correct @Shameel, I used the code here to read the information:

var lines = File.ReadAllLines(luaPath);


        foreach (var line in lines)
        {

            if (line.Contains("MAX_MONSTERS"))
            {
                Utilis.LogInfo("VARIABLE NAME: " + line.Split('=')[0].Trim());
                Utilis.LogInfo("VARIABLE VALUE: " + line.Split('=')[1].Trim());
                Console.WriteLine("------------------------------------------------------------------");
                TxtMaxMonsters.Text = line.Split('=')[1].Trim();
                TxtMaxMonsters.Enabled = true;

            }
         
            if (line.Contains("MIN_MONSTERS"))
            {
                Utilis.LogInfo("VARIABLE NAME: " + line.Split('=')[0].Trim());
                Utilis.LogInfo("VARIABLE VALUE: " + line.Split('=')[1].Trim());
                Console.WriteLine("------------------------------------------------------------------");
                TxtMinMonsters.Text = line.Split('=')[1].Trim();
                TxtMinMonsters.Enabled = true;

            }
         }

And I used the code below to save the modifications:

var lines = File.ReadAllLines(luaPath);
        for (var i = 0; i < lines.Length; i++)
        {
            var line = lines[i];

            if (line.Contains("MAX_MONSTERS"))
            {
                lines[i] = line.Replace(line.Split('=')[1].Trim(), TxtMaxMonsters.Text);
            }
            if (line.Contains("MIN_MONSTERS"))
            {
                lines[i] = line.Replace(line.Split('=')[1].Trim(), TxtMinMonsters.Text);
            }
            if (line.Contains("ITEMS_TO_DEPOSIT = {"))
            {
                lines[i] = line.Replace(line.Split('{')[1].Split('}')[0].Trim(), TxtItemsToDepositID.Text);
            }
        }
        File.WriteAllLines(luaPath, lines);