Skip to content

Instantly share code, notes, and snippets.

@sinbad
Created August 20, 2019 11:12
Show Gist options
  • Save sinbad/e11778a06019cc21c9b66f046c5f8207 to your computer and use it in GitHub Desktop.
Save sinbad/e11778a06019cc21c9b66f046c5f8207 to your computer and use it in GitHub Desktop.
private Dictionary<int, Action<Localisation.VariableSetter>> localisationVariableFuncs;
private void InitLocalisation() {
Localisation.Instance.DefaultVariableCallback = LocalisationVariableCallback;
localisationVariableFuncs = new Dictionary<int, Action<Localisation.VariableSetter>>{
{LocalisationConsts.Variables.ShotStyleBonusHash, setter => setter.SetValue(shotStyleBonuses)},
{LocalisationConsts.Variables.ShotPegScoreHash, setter => setter.SetValue(shotPegScore)}
};
// Iterate over all strings and pre-convert action tags to IDs so we only do this once
Localisation.Instance.ProcessMessages(InitLocalisationMessage);
}
private void InitLocalisationMessage(Localisation.Message msg) {
foreach (var variable in msg.Variables) {
if (variable.Type == Localisation.VariableType.Action) {
// We want to pre-calculate the actionID & preferred input type for speed later
ControllerGlyphs.Instance.AnalyseActionTag(variable.Name, out int actionId, out ControllerInputType preferredInput);
variable.userData = new[]{
actionId,
(int) preferredInput
};
}
}
}
private void LocalisationVariableCallback(Localisation.Variable v, Localisation.VariableSetter setter) {
switch (v.Type) {
case Localisation.VariableType.Manual:
LocalisationManualVariable(v, setter);
break;
case Localisation.VariableType.Action:
LocalisationActionVariable(v, setter);
break;
case Localisation.VariableType.Glyph:
LocalisationGlyphVariable(v, setter);
break;
}
}
private void LocalisationManualVariable(Localisation.Variable v, Localisation.VariableSetter setter) {
if (localisationVariableFuncs.TryGetValue(v.NameHash, out var f))
f(setter);
}
private void LocalisationActionVariable(Localisation.Variable v, Localisation.VariableSetter setter) {
// We pre-calculated the actionID and preferred input
if (v.userData != null && v.userData.Length >= 2) {
int actionID = v.userData[0];
ControllerInputType preferredInput = (ControllerInputType) v.userData[1];
int n = ControllerGlyphs.Instance.WriteActionSprites(actionID, preferredInput, setter.Buffer,
setter.BufferPos);
setter.BufferUpdated(n);
}
}
private void LocalisationGlyphVariable(Localisation.Variable v, Localisation.VariableSetter setter) {
int n = ControllerGlyphs.Instance.WriteGlyphSprite(v.Name, setter.Buffer, setter.BufferPos);
setter.BufferUpdated(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment