SHARE
TWEET

Untitled

a guest Jun 15th, 2016 8 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Reflection;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using Google2u;
  9.  
  10. namespace MyGame.Localisation {
  11.     public static class LocalisationFileGenerator {
  12.         const string localisationFileLocation = "0 - My Game/Scripts/Localisation/{0}.cs";
  13.         const string localisationFileNamespace = "MyGame.Localisation";
  14.         const string localisationFileName = "Localisation";
  15.         const string localisationEntryClassName = "TextLine";
  16.  
  17.         #region Templates
  18.         #region File
  19.         const string localisationFileTemplate =
  20.     @"//-------------------------------------------------
  21. //   This file has been automatically generated.
  22. //              Do not manually edit.
  23. //-------------------------------------------------
  24. namespace {0} {{
  25. {1}
  26.  
  27.    // The following classes represent each sheet in Google Docs.
  28.  
  29. {2}
  30. }}";
  31.         #endregion
  32.         #region Entry
  33.         const string localisationEntryClassTemplate =
  34.     @"    // This class is used for every line of text in the game. It contains all language variants, and an implicit operator,
  35.    // so a line can be referenced by its ID and automatically get the appropriate language. e.g.: 'Dialogue.LINE_ID'
  36.    public class {0} {{
  37. {1}
  38.  
  39.        public {0}({2}){{
  40. {3}
  41.        }}
  42.  
  43.        // This method should be modified in the localisationEntryClassTemplate in LocalisationFileGenerator.cs.
  44.        public static implicit operator string({0} {4}) {{
  45.            return {4}.English;
  46.        }}
  47.    }}";
  48.  
  49.         const string localisationEntryVariableTemplate = @"        public string {0};";
  50.         const string localisationEntryConstructorParameterTemplate = @"string {0}";
  51.         const string localisationEntryConstructorVariableAssignmentTemplate = @"            this.{0} = {0};";
  52.         #endregion
  53.         #region Group
  54.         const string localisationGroupClassTemplate =
  55.     @"    public static class {1} {{
  56. {2}
  57.    }}";
  58.  
  59.         const string localisationGroupEntryVariableTemplate = @"        public static {0} {1} = new {0}({2});";
  60.         #endregion
  61.         #endregion
  62.  
  63.         const string g2uDataLocation = "Google2uGen";
  64.  
  65.         [MenuItem("My Game/Generate Localisation File", false, 100000)]
  66.         public static void GenerateLocalisationFile() {
  67.             string g2uDataPath = Path.Combine(Application.dataPath, g2uDataLocation);
  68.  
  69.             if (!Directory.Exists(g2uDataPath)) {
  70.                 Debug.LogError("G2U data does not exist in default location. Ensure you have generated data files using G2U.");
  71.                 return;
  72.             }
  73.  
  74.             Type[] groups = GetAllIGoogle2uDBClasses();
  75.  
  76.             if (groups.Length == 0) {
  77.                 Debug.LogError("No classes that utilise the IGoogle2uDB interface found.");
  78.                 return;
  79.             }
  80.  
  81.             GenerateLocalisationFile(localisationFileName, localisationEntryClassName, groups.ToArray());
  82.  
  83.             FileUtil.DeleteFileOrDirectory(g2uDataPath);
  84.  
  85.             string g2uDataPathMetaFilePath = Application.dataPath + "/" + g2uDataLocation + ".meta";
  86.             if (File.Exists(g2uDataPathMetaFilePath)) {
  87.                 FileUtil.DeleteFileOrDirectory(g2uDataPathMetaFilePath);
  88.             }
  89.  
  90.             AssetDatabase.Refresh();
  91.  
  92.             Debug.Log("Localisation file generated successfully.");
  93.         }
  94.  
  95.         static Type[] GetAllIGoogle2uDBClasses() {
  96.             List<Type> classes = new List<Type>();
  97.  
  98.             Type[] types = Assembly.Load("Assembly-CSharp").GetTypes();
  99.  
  100.             foreach (Type type in types.Where(type => type.GetInterfaces().Contains(typeof(IGoogle2uDB)))) {
  101.                 if (type.GetInterfaces().Contains(typeof(IGoogle2uDB))) {
  102.                     if (type.Name != "Documentation" && type.Name != "Localization" && !classes.Contains(type)) {
  103.                         classes.Add(type);
  104.                     }
  105.                 }
  106.             }
  107.  
  108.             return classes.ToArray();
  109.         }
  110.  
  111.         static void GenerateLocalisationFile(string fileName, string textLineClassName, params Type[] groups) {
  112.             using (StreamWriter file = new StreamWriter(string.Format(Path.Combine(Application.dataPath, localisationFileLocation), fileName))) {
  113.                 string[] fieldNames = null;
  114.                 string groupsString = "";
  115.  
  116.                 for (int i = 0; i < groups.Length; i++) {
  117.                     IGoogle2uDB entryInstance = groups[i].GetProperty("Instance").GetValue(groups[i], null) as IGoogle2uDB;
  118.                     string[] rowNames = entryInstance.GetType().GetField("rowNames").GetValue(entryInstance) as string[];
  119.  
  120.                     if (fieldNames == null) {
  121.                         fieldNames = entryInstance.GetGenRow(rowNames[i]).GetType().GetFields()
  122.                                      .Select(field => field.Name)
  123.                                      .ToArray<string>();
  124.                     }
  125.  
  126.                     groupsString += GetGroupClassString(groups[i].Name, textLineClassName, entryInstance, rowNames, fieldNames);
  127.  
  128.                     if (i < groups.Length - 1) {
  129.                         groupsString += "\r\n\r\n";
  130.                     }
  131.                 }
  132.  
  133.                 file.Write(localisationFileTemplate, localisationFileNamespace, GetEntryClassString(textLineClassName, fieldNames), groupsString);
  134.             }
  135.         }
  136.  
  137.         /// <summary>Returns the code for a group/single sheet in Google Docs. One class is created for each sheet.</summary>
  138.         static string GetGroupClassString(string name, string textLineClassName, IGoogle2uDB typeClass, string[] rowNames, string[] fieldNames) {
  139.             string textLinesString = "";
  140.  
  141.             for (int i = 0; i < rowNames.Length; i++) {
  142.                 string fieldsString = "";
  143.                 IGoogle2uRow row = typeClass.GetGenRow(rowNames[i]);
  144.  
  145.                 for (int j = 0; j < fieldNames.Length; j++) {
  146.                     fieldsString += "\"" + row.GetType().GetField(fieldNames[j]).GetValue(row) + "\"";
  147.  
  148.                     if (j < fieldNames.Length - 1) {
  149.                         fieldsString += @", ";
  150.                     }
  151.                 }
  152.  
  153.                 textLinesString += string.Format(localisationGroupEntryVariableTemplate, textLineClassName, rowNames[i], fieldsString);
  154.  
  155.                 if (i < rowNames.Length - 1) {
  156.                     textLinesString += "\r\n";
  157.                 }
  158.             }
  159.  
  160.             return string.Format(localisationGroupClassTemplate, localisationFileNamespace, name, textLinesString);
  161.         }
  162.  
  163.         /// <summary>Returns the code for a line in Google Docs. This class is shared across all groups.</summary>
  164.         static string GetEntryClassString(string name, string[] fieldNames) {
  165.             string rowVariables = "";
  166.             string rowConstructorParams = "";
  167.             string rowConstructorVariableAssignments = "";
  168.  
  169.             for (int i = 0; i < fieldNames.Length; i++) {
  170.                 string fieldName = fieldNames[i].Replace("_", "");
  171.  
  172.                 rowVariables += string.Format(localisationEntryVariableTemplate, fieldName);
  173.                 rowConstructorParams += string.Format(localisationEntryConstructorParameterTemplate, fieldName);
  174.                 rowConstructorVariableAssignments += string.Format(localisationEntryConstructorVariableAssignmentTemplate, fieldName);
  175.  
  176.                 if (i < fieldNames.Length - 1) {
  177.                     rowVariables += "\r\n";
  178.                     rowConstructorParams += @", ";
  179.                     rowConstructorVariableAssignments += "\r\n";
  180.                 }
  181.             }
  182.  
  183.             return string.Format(localisationEntryClassTemplate, name, rowVariables, rowConstructorParams, rowConstructorVariableAssignments, char.ToLowerInvariant(name[0]) + name.Substring(1));
  184.         }
  185.     }
  186. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top