1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | // // VOID_Config.cs // // Author: // toadicus <> // // Copyright (c) 2013 toadicus // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. using System; using System.Collections.Generic; using KSP; using UnityEngine; namespace VOID { public struct VOID_SaveValue<T> : IVOID_SaveValue { private T _value; private Type _type; private VOID_Core Core { get { if (HighLogic.LoadedSceneIsEditor) { if (VOID_EditorCore.Initialized) { return VOID_EditorCore.Instance; } } else if (HighLogic.LoadedSceneIsFlight) { if (VOID_Core.Initialized) { return VOID_Core.Instance; } } return null; } } public T value { get { return this._value; } set { if (this.Core != null && !System.Object.Equals(this._value, value)) { Tools.PostDebugMessage (string.Format ( "VOID: Dirtying config for type {0} in method {1}." + "\n\t Old Value: {2}, New Value: {3}" + "\n\t Object.Equals(New, Old): {4}", this._type, new System.Diagnostics.StackTrace().GetFrame(1).GetMethod(), this._value, value, System.Object.Equals(this._value, value) )); this.Core.configDirty = true; } this._value = value; } } public Type type { get { if (this._type == null && this._value != null) { this._type = this._value.GetType (); } return this._type; } set { this._type = value; } } public object AsType { get { return (T)this._value; } } public void SetValue(object v) { this.value = (T)v; } public static implicit operator T(VOID_SaveValue<T> v) { return (T)v.value; } public static implicit operator VOID_SaveValue<T>(T v) { VOID_SaveValue<T> r = new VOID_SaveValue<T>(); r.type = v.GetType(); r.value = v; return r; } public override string ToString() { return this.value.ToString(); } } public interface IVOID_SaveValue { Type type { get; } object AsType { get; } void SetValue(object v); } [AttributeUsage(AttributeTargets.Field)] public class AVOID_SaveValue : Attribute { protected string _name; public string Name { get { return this._name; } } public AVOID_SaveValue(string fieldName) { this._name = fieldName; } } } |