I2 Loc and asset bundles (MOD)

More
7 years 5 months ago - 7 years 5 months ago #2337 by progm
Hi there,
I'm using asset bundles to manage "MODs" for my games.
Players can download MODs (that are indeed asset bundles), put them in a certain folder and, if they are selected in the MOD menu, they are loaded in runtime.

When a MOD is loaded, I need to replace certain translations (dialogues) with others.
How can I achieve this behavior?

I tried to create a new LanguageSource prefab and putting it in the asset bundle. However I'm having two issues:

* The Source is always loaded in the editor as secondary language, so I can't test properly if it would be loaded correctly from the asset bundle.
* The Source is loaded as secondary, so it doesn't "overwrites" the primary one. I could "fix" it by calling I2.Loc.LocalizationManager.Sources.Reverse (), but it's very bad I think.

What would you suggest me?

Thank you very much!
Last edit: 7 years 5 months ago by progm.

Please Log in or Create an account to join the conversation.

More
7 years 5 months ago #2340 by Frank
Hi,
If you add another LanguageSource, it will work if you add the LanguageSource to your scene. It will be loaded as soon as you load any of the scenes, and its terms will be used.

However, I don't advise that route, because you will have boths terms (the one in the new languageSource and the one in I2Languages.prefab). So, it will not OVERRIDE the translations.

My advise is to export the LanguageSource as a CSV or I2CSV file (using the Spreadsheet tab in the LanguageSource). And include that CSV, I2CSV file in your AssetBundle.
Then, when you load your bundle, just Import the CSV data. That will override whatever is in the I2Languages.prefab, and can even add new terms, replace exciting ones.

Just call
string csv = .... get csv text from your asset bundle ....
LocalizationManager.Sources[0].ImportCSV(csv);

Hope that helps,
Frank

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out
The following user(s) said Thank You: progm

Please Log in or Create an account to join the conversation.

More
7 years 5 months ago #2341 by progm
Wow, thank you, this is perfect!

However, how can I "unload" the translations replaced in this way? For instance, I need to unload the replaced translations when the MOD is disabled on runtime.

Maybe I can just Destroy() the Source and then run LanguageSource.UpdateSources() to reload it?

Please Log in or Create an account to join the conversation.

More
7 years 5 months ago #2350 by Frank
You could always cache the default localization and restore it after:
static string mCached_DefaultLocalization;
.....

// To enable the mod localization:
mCached_DefaultLocalization = LocalizationManager.Sources[0].Export_I2CSV(null);
string csv = .... get new csv text from your asset bundle ....
LocalizationManager.Sources[0].ImportCSV(csv);


// to restore the original one:
LocalizationManager.Sources[0].Import_I2CSV(null, mCached_DefaultLocalization);

Hope that helps,
Frank

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out

Please Log in or Create an account to join the conversation.

More
7 years 4 months ago - 7 years 4 months ago #2357 by progm
Hey,
I tried to implement this, but I'm having a bunch of issues :(

First of all, if I close the game in the editor after loading a MOD with a translation, the editor keeps showing me the new replaced translations instead of the originals.

The only way to fix is to close the editor and re-open.

I also tried to force the editor to reload the sources from disk in any way:
LocalizationManager.Sources.Clear ();
ResourceManager.pInstance.Assets = new Object[] { };
ResourceManager.pInstance.CleanResourceCache ();
LocalizationManager.UpdateSources ();

But no change: once a MOD is loaded, there's no way to reset the current source till the next restart.


Another way could be to capture the OnApplicationExit() event and reload everything from the cached CSV, but I think it's not very safe...

Any suggestions?

Thank you very much!

EDIT:

More news: I tried also with the `OnApplicationExit()` approach, but I get only more bugs. Now after reloading from the cached CSV, the game ignores any further call of LocalizationManager.Sources[0].ImportCSV.
Last edit: 7 years 4 months ago by progm. Reason: Adding more infos

Please Log in or Create an account to join the conversation.

More
7 years 4 months ago - 7 years 4 months ago #2361 by progm
Sorry for double posting.

I think I found a working solution.

The idea is to create a language source in runtime, putting it as the first of all language sources, and load the csv in it.

Then, to unload the translations I just have to delete it.

This is the script to load mods:
var translations = CurrentBundle.LoadAsset<TextAsset> ("translations.cvs");
if (translations != null) {
  List<string[]> csv = LocalizationReader.ReadCSV(translations.text);
  var o = new GameObject ("Workshop");
  var source = o.AddComponent<LanguageSource> ();
  source.Import_CSV (null, csv);
  if (LocalizationManager.Sources[LocalizationManager.Sources.Count - 1] == source) {
    LocalizationManager.Sources.Reverse();  
  }
}

And there's is how I unload it:
if (LocalizationManager.Sources.Count > 1) {

  foreach (var source in LocalizationManager.Sources) {
    if (source.name == "Workshop") {
      DestroyImmediate (source);
    }
  }
  LocalizationManager.UpdateSources ();
}


Do you think that this could deal to any issue?
Last edit: 7 years 4 months ago by progm.

Please Log in or Create an account to join the conversation.

Time to create page: 0.152 seconds
Template by JoomlaShine