Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slightly clean up externalizeUTWithJson #1531

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 52 additions & 95 deletions UndertaleModTool/Scripts/Community Scripts/UndertaleWithJSONs.csx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ int maxCount = 1;

EnsureDataLoaded();

if (Data?.GeneralInfo?.DisplayName?.Content.ToLower() == "deltarune chapter 1 & 2")
var displayName = Data.GeneralInfo?.DisplayName?.Content.ToLower();

if (displayName == "deltarune chapter 1 & 2" || displayName == "deltarune chapter 1&2")
{
ScriptError("Error 0: Incompatible with the new Deltarune Chapter 1 & 2 demo");
return;
}
else if (Data?.GeneralInfo?.DisplayName?.Content.ToLower() == "deltarune chapter 1&2")
{
ScriptError("Error 1: Incompatible with the new Deltarune Chapter 1 & 2 demo");
return;
}


string langFolder = GetFolder(FilePath) + "lang" + Path.DirectorySeparatorChar;
string langFolder = Path.Combine(Path.GetDirectoryName(FilePath), "lang");
ThreadLocal<GlobalDecompileContext> DECOMPILE_CONTEXT = new ThreadLocal<GlobalDecompileContext>(() => new GlobalDecompileContext(Data, false));

if (Directory.Exists(langFolder))
Expand Down Expand Up @@ -58,44 +55,27 @@ void IncProgressLocal()
IncrementProgress();
}

string GetFolder(string path) {
return Path.GetDirectoryName(path) + Path.DirectorySeparatorChar;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////

ImportGMLString("gml_Script_scr_change_language", @"");
ImportGMLString("gml_Script_scr_84_load_map_json", @"");
if (Data.GeneralInfo.Major < 2) // Undertale PC (GMS1)
{
ImportGMLString("gml_Script_textdata_en", @"
if (variable_global_exists(""text_data_en""))
ds_map_destroy(global.text_data_en);
global.text_data_en = scr_84_load_map_json(program_directory + ""\lang\"" + ""lang_en.json"");");
ImportGMLString("gml_Script_textdata_ja", @"
if (variable_global_exists(""text_data_ja""))
ds_map_destroy(global.text_data_ja);
global.text_data_ja = scr_84_load_map_json(program_directory + ""\lang\"" + ""lang_ja.json"");");
}
else
{
ImportGMLString("gml_Script_textdata_en", @"
if (variable_global_exists(""text_data_en""))
ds_map_destroy(global.text_data_en);
global.text_data_en = scr_84_load_map_json(program_directory + ""\lang\\"" + ""lang_en.json"");");
ImportGMLString("gml_Script_textdata_ja", @"
if (variable_global_exists(""text_data_ja""))
ds_map_destroy(global.text_data_ja);
global.text_data_ja = scr_84_load_map_json(program_directory + ""\lang\\"" + ""lang_ja.json"");");
}
Comment on lines -69 to -90
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked if ""\lang\\"" can be ""\lang\"" for GMS 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldnt matter.
it essentially wants to do program directory + \lang\ + lang_ja.json (or lang_en). Hence why I replaced it with a slash, as that then also works for all operating systems properly instead of just Windows. and these two paths program_directory/lang or program_directory/////lang are the same.


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Undertale PC (GMS1)
ImportGMLString("gml_Script_textdata_en", @"
if (variable_global_exists(""text_data_en""))
ds_map_destroy(global.text_data_en);
global.text_data_en = scr_84_load_map_json(program_directory + ""/lang/"" + ""lang_en.json"");");
ImportGMLString("gml_Script_textdata_ja", @"
if (variable_global_exists(""text_data_ja""))
ds_map_destroy(global.text_data_ja);
global.text_data_ja = scr_84_load_map_json(program_directory + ""/lang/"" + ""lang_ja.json"");");

//////////////////////////////////////////////////////

Data.GameObjects.ByName("obj_time").EventHandlerFor(EventType.KeyPress, EventSubtypeKey.vk_f11, Data.Strings, Data.Code, Data.CodeLocals).ReplaceGML(@"
scr_change_language()
", Data);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////

Data.GameObjects.ByName("obj_time").EventHandlerFor(EventType.KeyPress, EventSubtypeKey.vk_f12, Data.Strings, Data.Code, Data.CodeLocals).ReplaceGML(@"
if (global.language == ""en"")
Expand All @@ -104,10 +84,10 @@ else
textdata_ja()
", Data);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////

ImportGMLString("gml_Script_scr_change_language", @"
//Read the language from the INI file
ImportGMLString("gml_Script_scr_change_language", @"
// Read the language from the INI file
if (global.language == ""en"")
global.language = ""ja""
else
Expand All @@ -117,9 +97,9 @@ ini_write_string(""General"", ""lang"", global.language)
ossafe_ini_close()
");

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////

ImportGMLString("gml_Script_scr_84_load_map_json", @"
ImportGMLString("gml_Script_scr_84_load_map_json", @"
var filename = argument0
var file = file_text_open_read(filename)
var json = """"
Expand All @@ -132,14 +112,14 @@ return json_decode(json);
HideProgressBar();
ScriptMessage("Complete.");

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////

void DumpJSON(string language)
{
var lang_file = Data.Code.ByName("gml_Script_textdata_" + language);
try
{
File.WriteAllText(Path.Combine(langFolder, "lang_" + language + ".json"), (lang_file != null ? Decompiler.Decompile(lang_file, DECOMPILE_CONTEXT.Value) : ""));
File.WriteAllText(Path.Combine(langFolder, "lang_" + language + ".json"), (lang_file is not null ? Decompiler.Decompile(lang_file, DECOMPILE_CONTEXT.Value) : ""));
}
catch (Exception e)
{
Expand All @@ -150,72 +130,49 @@ void DumpJSON(string language)
UpdateProgressValue(GetProgress());
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////

void MakeJSON(string language)
{
if (Data.GeneralInfo.Major < 2) // Undertale PC (GMS1)
// Undertale PC (GMS1)
string input = File.ReadAllText(Path.Combine(langFolder, "lang_" + language + ".json"));

string pattern = ".*ds_map_create\\(\\)";
string replacement = "{";
input = Regex.Replace(input, pattern, replacement);

// Undertale Switch/Probs other consoles (GMS2)
if (Data.GeneralInfo.Major >= 2)
{
string input = File.ReadAllText(Path.Combine(langFolder, "lang_" + language + ".json"));

string pattern = ".*ds_map_create\\(\\)";
string replacement = "{";
input = Regex.Replace(input, pattern, replacement);

pattern = @"\\";
replacement = @"\\";
input = Regex.Replace(input, pattern, replacement);

pattern = @""" \+ '""' \+ """;
replacement = @"\""";
input = Regex.Replace(input, pattern, replacement);

pattern = @"'""' \+ """;
replacement = @"""\""";
input = Regex.Replace(input, pattern, replacement);

pattern = @""" \+ '""'";
replacement = @"\""""";
input = Regex.Replace(input, pattern, replacement);

pattern = @"'""'";
replacement = @"\""";
input = Regex.Replace(input, pattern, replacement);

input = input.Replace(@"\"",", @"\"""",");

pattern = @"ds_map_add\(global\.text_data_.., ("".*""), ("".*"")\)";
replacement = @" $1: $2,";
input = Regex.Replace(input, pattern, replacement);

pattern = @",\n\Z";
replacement = "\n}";
input = Regex.Replace(input, pattern, replacement);

File.WriteAllText(Path.Combine(langFolder, "lang_" + language + ".json"), input);

IncProgressLocal();
UpdateProgressValue(GetProgress());
}
else // Undertale Switch/Probs other consoles (GMS2)
{
string input = File.ReadAllText(Path.Combine(langFolder, "lang_" + language + ".json"));

string pattern = ".*ds_map_create\\(\\)";
string replacement = "{";
input = Regex.Replace(input, pattern, replacement);

pattern = @"ds_map_add\(global\.text_data_.., ("".*""), ("".*"")\)";
replacement = @" $1: $2,";
input = Regex.Replace(input, pattern, replacement);

pattern = @",\n\Z";
replacement = "\n}";
input = Regex.Replace(input, pattern, replacement);

File.WriteAllText(Path.Combine(langFolder, "lang_" + language + ".json"), input);
Comment on lines -157 to -216
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same question here as for lines 69-90


IncProgressLocal();
UpdateProgressValue(GetProgress());
}
}

pattern = @"ds_map_add\(global\.text_data_.., ("".*""), ("".*"")\)";
replacement = @" $1: $2,";
input = Regex.Replace(input, pattern, replacement);

pattern = @",\n\Z";
replacement = "\n}";
input = Regex.Replace(input, pattern, replacement);

File.WriteAllText(Path.Combine(langFolder, "lang_" + language + ".json"), input);

IncProgressLocal();
UpdateProgressValue(GetProgress());
}
Loading