private void VisitIn(DOMEditorPrefsProperty[] editorPrefsProperties) { if (editorPrefsProperties == null) { return; } var prefsToBind = new List <DOMEditorPrefsProperty>(); for (int i = 0; i < editorPrefsProperties.Length; i++) { var pref = editorPrefsProperties[i]; Assert.IsFalse(string.IsNullOrEmpty(pref.name), "name is required for field <editorPrefs />"); Assert.AreNotEqual(DOMEditorPrefsProperty.Type.None, pref.type, "type is required for field <editorPrefs />"); var type = string.Empty; var setter = string.Empty; var getter = string.Empty; switch (pref.type) { case DOMEditorPrefsProperty.Type.Bool: setter = string.Format("EditorPrefs.SetInt(\"{0}.{1}\", value ? 1 : 0);", m_Class.name, pref.name); getter = string.Format( "return EditorPrefs.GetInt(\"{0}.{1}\", {2}) == 1;", m_Class.name, pref.name, string.IsNullOrEmpty(pref.@default) ? "false" : pref.@default); type = "bool"; break; case DOMEditorPrefsProperty.Type.Int: setter = string.Format("EditorPrefs.SetInt(\"{0}.{1}\", value);", m_Class.name, pref.name); getter = string.Format( "return EditorPrefs.GetInt(\"{0}.{1}\", {2});", m_Class.name, pref.name, string.IsNullOrEmpty(pref.@default) ? "0" : pref.@default); type = "int"; break; case DOMEditorPrefsProperty.Type.String: setter = string.Format("EditorPrefs.SetString(\"{0}.{1}\", value);", m_Class.name, pref.name); getter = string.Format( "return EditorPrefs.GetString(\"{0}.{1}\", {2});", m_Class.name, pref.name, string.IsNullOrEmpty(pref.@default) ? "string.Empty" : "\"" + pref.@default + "\""); type = "string"; break; case DOMEditorPrefsProperty.Type.Enum: Assert.IsFalse(string.IsNullOrEmpty(pref.customType), "customType is required for type 'enum' in <editorPrefs />"); setter = string.Format("EditorPrefs.SetInt(\"{0}.{1}\", (int)value);", m_Class.name, pref.name); getter = string.Format( "return ({3})EditorPrefs.GetInt(\"{0}.{1}\", {2});", m_Class.name, pref.name, string.IsNullOrEmpty(pref.@default) ? "default(" + pref.customType + ")" : pref.@default, pref.customType); type = pref.customType; break; } m_Class.AddProperty(new CSProperty(Scope.Internal, pref.name, type) .SetSetter(setter) .SetGetter(getter)); if (m_HasService && !string.IsNullOrEmpty(pref.vmPropertyName)) { m_AwakeMethod.AddBodyLine("m_Service.vm." + pref.vmPropertyName + " = " + pref.name); if (m_HasRoot) { prefsToBind.Add(pref); } } } if (prefsToBind.Count > 0) { m_OnVMPropertyChanged.AddBodyLine("switch (propertyChangedEventArgs.PropertyName)"); m_OnVMPropertyChanged.AddBodyLine("{"); foreach (var pref in prefsToBind) { m_OnVMPropertyChanged.AddBodyLine(" case \"" + pref.vmPropertyName + "\": " + pref.name + " = m_Service.vm." + pref.vmPropertyName + "; break;"); } m_OnVMPropertyChanged.AddBodyLine("}"); } }
void GeneratePropertyInternal(string type, string name, string fieldName, string pptName, string getterContent, string setterContent, string fieldType = null) { var csProperty = new CSProperty(Scope.Public, name, type); m_Class.AddProperty(csProperty); if (string.IsNullOrEmpty(getterContent)) { csProperty.SetGetter("return (" + type + ")" + fieldName + ";"); } else { csProperty.SetGetter(kVariableReference.Replace(getterContent, "$1") + ";"); } var hasSetter = !string.IsNullOrEmpty(setterContent) || string.IsNullOrEmpty(getterContent) && string.IsNullOrEmpty(setterContent); if (hasSetter) { var setterMethod = new CSMethod(Scope.Private, "bool", "Set" + NameUtility.PascalCase(name)) .AddArgument(type, "value"); m_Class.AddMethod(setterMethod); if (!string.IsNullOrEmpty(setterContent)) { setterMethod.AddBodyLine(setterContent); } else { var fieldTypeCast = (string.IsNullOrEmpty(fieldType) ? "" : "(" + fieldType + ")"); setterMethod .AddBodyLine("if (" + fieldName + " != " + fieldTypeCast + "value)") .AddBodyLine("{") .AddBodyLine(" " + fieldName + " = " + fieldTypeCast + "value;") .AddBodyLine(" return true;") .AddBodyLine("}") .AddBodyLine("return false;"); } var dependentProperties = new HashSet <string>() { name }; if (m_PropertyGetterDependencies.ContainsKey(name)) { dependentProperties.UnionWith(m_PropertyGetterDependencies[name]); } var properties = dependentProperties.Select(p => "Properties." + NameUtility.SlugifyConstName(p)).Aggregate((l, r) => l + ", " + r); csProperty.SetSetter("if (" + setterMethod.name + "(value)) { SetPropertyChanged(" + properties + "); }"); } m_PropertyClass.AddField( new CSField( Scope.Public, pptName, "ClassProperty<" + type + ">", "new StaticClassProperty<" + type + ", " + m_Class.name + ">(\"" + name + "\")", CSField.Modifier.Static | CSField.Modifier.Readonly)); }