/// <summary> /// Load the configuration inline if the callback is null. /// </summary> public void Load(IAction <Configuration> onLoad = null) { _lock.Take(); // check state switch (State) { case AssetState.Unloaded: case AssetState.Broken: State = AssetState.Loading; // inline load or callback? if (onLoad == null) { // no callback so load immediately // get the connection ConnectionLocal connection = ManagerConnections.Get <ConnectionLocal>(_path); // ensure the file modes are right connection.FileMode = System.IO.FileMode.OpenOrCreate; connection.FileAccess = System.IO.FileAccess.Read; // get the stream Teple <LockShared, ByteBuffer> resource; connection.Get(out resource); // run load logic OnLoad(resource.ArgB); resource.ArgA.Release(); } else { _onLoad += onLoad; // get the connection ConnectionLocal connection = ManagerConnections.Get <ConnectionLocal>(_path); // ensure the file modes are right connection.FileMode = System.IO.FileMode.OpenOrCreate; connection.FileAccess = System.IO.FileAccess.Read; // callback load connection.Get(new Act <ByteBuffer>(OnLoad)); } break; case AssetState.Loading: _onLoad += onLoad; _lock.Release(); break; default: _lock.Release(); onLoad.ArgA = this; onLoad.Run(); break; } }
//-------------------------------------------// /// <summary> /// On connection. /// </summary> protected void OnConnection(ConnectionLocal connection) { // reset the timeout _timeout.Reset(Timeout); // persist the connection _connection = connection; // get a stream to the connection _connection.Get(new Act <ByteBuffer>(Read)); }
/// <summary> /// Save the configuration. /// </summary> public void Save(Act <Configuration> onSave = null) { _lock.Take(); if (State.Is(AssetState.Loaded) || State.Is(AssetState.Unloaded)) { State = AssetState.Saving; if (onSave == null) { // get the connection ConnectionLocal connection = ManagerConnections.Get <ConnectionLocal>(_path); // ensure the file modes are right connection.FileMode = System.IO.FileMode.Truncate; connection.FileAccess = System.IO.FileAccess.Write; connection.FileShare = System.IO.FileShare.Read; // get the stream Teple <LockShared, ByteBuffer> resource; connection.Get(out resource); // run save logic OnSave(resource.ArgB); resource.ArgA.Release(); } else { onSave.ArgA = this; // get the connection ConnectionLocal connection = ManagerConnections.Get <ConnectionLocal>(_path); // ensure the file modes are right connection.FileMode = System.IO.FileMode.Create; connection.FileAccess = System.IO.FileAccess.Write; // callback save connection.Get(new Act <ByteBuffer>(new ActionPair <ByteBuffer>(OnSave, s => onSave.Run()))); } } else { _lock.Release(); } }