Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System;
- using System.Reflection;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using Google2u;
- namespace MyGame.Localisation {
- public static class LocalisationFileGenerator {
- const string localisationFileLocation = "0 - My Game/Scripts/Localisation/{0}.cs";
- const string localisationFileNamespace = "MyGame.Localisation";
- const string localisationFileName = "Localisation";
- const string localisationEntryClassName = "TextLine";
- #region Templates
- #region File
- const string localisationFileTemplate =
- @"//-------------------------------------------------
- // This file has been automatically generated.
- // Do not manually edit.
- //-------------------------------------------------
- namespace {0} {{
- {1}
- // The following classes represent each sheet in Google Docs.
- {2}
- }}";
- #endregion
- #region Entry
- const string localisationEntryClassTemplate =
- @" // This class is used for every line of text in the game. It contains all language variants, and an implicit operator,
- // so a line can be referenced by its ID and automatically get the appropriate language. e.g.: 'Dialogue.LINE_ID'
- public class {0} {{
- {1}
- public {0}({2}){{
- {3}
- }}
- // This method should be modified in the localisationEntryClassTemplate in LocalisationFileGenerator.cs.
- public static implicit operator string({0} {4}) {{
- return {4}.English;
- }}
- }}";
- const string localisationEntryVariableTemplate = @" public string {0};";
- const string localisationEntryConstructorParameterTemplate = @"string {0}";
- const string localisationEntryConstructorVariableAssignmentTemplate = @" this.{0} = {0};";
- #endregion
- #region Group
- const string localisationGroupClassTemplate =
- @" public static class {1} {{
- {2}
- }}";
- const string localisationGroupEntryVariableTemplate = @" public static {0} {1} = new {0}({2});";
- #endregion
- #endregion
- const string g2uDataLocation = "Google2uGen";
- [MenuItem("My Game/Generate Localisation File", false, 100000)]
- public static void GenerateLocalisationFile() {
- string g2uDataPath = Path.Combine(Application.dataPath, g2uDataLocation);
- if (!Directory.Exists(g2uDataPath)) {
- Debug.LogError("G2U data does not exist in default location. Ensure you have generated data files using G2U.");
- return;
- }
- Type[] groups = GetAllIGoogle2uDBClasses();
- if (groups.Length == 0) {
- Debug.LogError("No classes that utilise the IGoogle2uDB interface found.");
- return;
- }
- GenerateLocalisationFile(localisationFileName, localisationEntryClassName, groups.ToArray());
- FileUtil.DeleteFileOrDirectory(g2uDataPath);
- string g2uDataPathMetaFilePath = Application.dataPath + "/" + g2uDataLocation + ".meta";
- if (File.Exists(g2uDataPathMetaFilePath)) {
- FileUtil.DeleteFileOrDirectory(g2uDataPathMetaFilePath);
- }
- AssetDatabase.Refresh();
- Debug.Log("Localisation file generated successfully.");
- }
- static Type[] GetAllIGoogle2uDBClasses() {
- List<Type> classes = new List<Type>();
- Type[] types = Assembly.Load("Assembly-CSharp").GetTypes();
- foreach (Type type in types.Where(type => type.GetInterfaces().Contains(typeof(IGoogle2uDB)))) {
- if (type.GetInterfaces().Contains(typeof(IGoogle2uDB))) {
- if (type.Name != "Documentation" && type.Name != "Localization" && !classes.Contains(type)) {
- classes.Add(type);
- }
- }
- }
- return classes.ToArray();
- }
- static void GenerateLocalisationFile(string fileName, string textLineClassName, params Type[] groups) {
- using (StreamWriter file = new StreamWriter(string.Format(Path.Combine(Application.dataPath, localisationFileLocation), fileName))) {
- string[] fieldNames = null;
- string groupsString = "";
- for (int i = 0; i < groups.Length; i++) {
- IGoogle2uDB entryInstance = groups[i].GetProperty("Instance").GetValue(groups[i], null) as IGoogle2uDB;
- string[] rowNames = entryInstance.GetType().GetField("rowNames").GetValue(entryInstance) as string[];
- if (fieldNames == null) {
- fieldNames = entryInstance.GetGenRow(rowNames[i]).GetType().GetFields()
- .Select(field => field.Name)
- .ToArray<string>();
- }
- groupsString += GetGroupClassString(groups[i].Name, textLineClassName, entryInstance, rowNames, fieldNames);
- if (i < groups.Length - 1) {
- groupsString += "\r\n\r\n";
- }
- }
- file.Write(localisationFileTemplate, localisationFileNamespace, GetEntryClassString(textLineClassName, fieldNames), groupsString);
- }
- }
- /// <summary>Returns the code for a group/single sheet in Google Docs. One class is created for each sheet.</summary>
- static string GetGroupClassString(string name, string textLineClassName, IGoogle2uDB typeClass, string[] rowNames, string[] fieldNames) {
- string textLinesString = "";
- for (int i = 0; i < rowNames.Length; i++) {
- string fieldsString = "";
- IGoogle2uRow row = typeClass.GetGenRow(rowNames[i]);
- for (int j = 0; j < fieldNames.Length; j++) {
- fieldsString += "\"" + row.GetType().GetField(fieldNames[j]).GetValue(row) + "\"";
- if (j < fieldNames.Length - 1) {
- fieldsString += @", ";
- }
- }
- textLinesString += string.Format(localisationGroupEntryVariableTemplate, textLineClassName, rowNames[i], fieldsString);
- if (i < rowNames.Length - 1) {
- textLinesString += "\r\n";
- }
- }
- return string.Format(localisationGroupClassTemplate, localisationFileNamespace, name, textLinesString);
- }
- /// <summary>Returns the code for a line in Google Docs. This class is shared across all groups.</summary>
- static string GetEntryClassString(string name, string[] fieldNames) {
- string rowVariables = "";
- string rowConstructorParams = "";
- string rowConstructorVariableAssignments = "";
- for (int i = 0; i < fieldNames.Length; i++) {
- string fieldName = fieldNames[i].Replace("_", "");
- rowVariables += string.Format(localisationEntryVariableTemplate, fieldName);
- rowConstructorParams += string.Format(localisationEntryConstructorParameterTemplate, fieldName);
- rowConstructorVariableAssignments += string.Format(localisationEntryConstructorVariableAssignmentTemplate, fieldName);
- if (i < fieldNames.Length - 1) {
- rowVariables += "\r\n";
- rowConstructorParams += @", ";
- rowConstructorVariableAssignments += "\r\n";
- }
- }
- return string.Format(localisationEntryClassTemplate, name, rowVariables, rowConstructorParams, rowConstructorVariableAssignments, char.ToLowerInvariant(name[0]) + name.Substring(1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement