public override async Task <InitResult> Initialize(InitParameter parameter, AdapterCallback callback) { this.callback = callback; string code = parameter.Calculation.Definition; dt = parameter.Calculation.Cycle; if (!string.IsNullOrWhiteSpace(code)) { // We need to lock the init code in order to prevent concurrent compilation of csharp-libraries! bool lockWasTaken = false; object lockObj = handleInitLock; try { Monitor.Enter(lockObj, ref lockWasTaken); return(await DoInit(parameter, code)); } finally { if (lockWasTaken) { Monitor.Exit(lockObj); } } } else { return(new InitResult() { Inputs = new InputDef[0], Outputs = new OutputDef[0], States = new StateDef[0], ExternalStatePersistence = true }); } }
//------------------------------------------ // 初期化 //------------------------------------------ #region ===== INITIALIZE ===== public void Initialize(InitParameter _param) { Debug.Assert(_param != null); Debug.Assert(_param?.MainCamera != null); Debug.Assert(_param?.MoveTarget != null); m_moveTarget = _param.MoveTarget; m_mainCamera = _param.MainCamera; }
//----------------------------------------- // 初期化 //----------------------------------------- #region ===== INITIALIZE ===== /// <summary> /// 初期化メソッド /// 継承先から呼んでもらおう /// </summary> /// <param name="_param"></param> protected void Initialize(InitParameter _param) { if (_param == null) { return; } //StateMachine 初期化 InitMachine(); }
// /* Sound */ // [SerializeField] // private TouchSound m_touchSound; // protected TouchSound Sound{get{return m_touchSound;}} #endregion //) ===== MEMBER_VATRIABLES ===== //----------------------------------------- // 初期化 //----------------------------------------- #region ===== INITIALIZE ===== /// <summary> /// 初期化メソッド /// </summary> protected void Init(InitParameter _param) { if (m_button == null) { m_button = GetComponent <Button>(); // 長押し判定もあるため、onClick にはEvent登録を行わない // (自前で呼ぶ) } if (_param == null) { return; } m_itemIndex = _param.ItemIndex; m_onButtonClicked = _param.OnButtonClicked; m_onButtonLongTapped = _param.OnButtonLongTapped; m_onFlickCallback = _param.OnFlickCallback; }
private async Task <InitResult> DoInit(InitParameter parameter, string code) { var config = new Mediator.Config(parameter.ModuleConfig); string libs = config.GetOptionalString("csharp-libraries", ""); bool cache = config.GetOptionalBool("csharp-cache-scripts", true); string[] assemblies = libs .Split(new char[] { ';', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .ToArray(); string[] absoluteAssemblies = assemblies.Select(d => Path.GetFullPath(d)).ToArray(); foreach (string assembly in absoluteAssemblies) { if (!File.Exists(assembly)) { throw new Exception($"csharp-library does not exist: {assembly}"); } } absoluteAssemblies = absoluteAssemblies.Select(assembly => { if (assembly.ToLowerInvariant().EndsWith(".cs")) { CompileResult compileRes = CompileLib.CSharpFile2Assembly(assembly); Print(compileRes, assembly); return(compileRes.AssemblyFileName); } return(assembly); }).ToArray(); var referencedAssemblies = new List <Assembly>(); foreach (string assembly in absoluteAssemblies) { try { Assembly ass = Assembly.LoadFrom(assembly); referencedAssemblies.Add(ass); } catch (Exception exp) { throw new Exception($"Failed to load csharp-library {assembly}: {exp.Message}"); } } CodeToObjectBase objMaker; if (cache) { objMaker = new CodeToObjectCompile(); } else { objMaker = new CodeToObjectScripting(); } object obj = await objMaker.MakeObjectFromCode(parameter.Calculation.Name, code, referencedAssemblies); inputs = GetIdentifiableMembers <InputBase>(obj, "", recursive: false).ToArray(); outputs = GetIdentifiableMembers <OutputBase>(obj, "", recursive: false).ToArray(); states = GetIdentifiableMembers <AbstractState>(obj, "", recursive: true).ToArray(); var eventProviders = GetMembers <EventProvider>(obj, recursive: true); foreach (EventProvider provider in eventProviders) { provider.EventSinkRef = this; } Type type = obj.GetType(); MethodInfo[] methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) .Where(m => m.Name == "Step" && IsStepSignature(m)) .ToArray(); if (methods.Length == 0) { throw new Exception("No Step(Timestamp t, TimeSpan dt) method found."); } MethodInfo step = methods[0]; stepAction = (Action <Timestamp, Duration>)step.CreateDelegate(typeof(Action <Timestamp, Duration>), obj); foreach (StateValue v in parameter.LastState) { AbstractState state = states.FirstOrDefault(s => s.ID == v.StateID); if (state != null) { state.SetValueFromDataValue(v.Value); } } return(new InitResult() { Inputs = inputs.Select(MakeInputDef).ToArray(), Outputs = outputs.Select(MakeOutputDef).ToArray(), States = states.Select(MakeStateDef).ToArray(), ExternalStatePersistence = true }); }