public void Init(Action callback) { Task.Factory.StartNew(() => { bool isWork = Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase); if (isWork) // 是作业 { DoInit(isWork, callback); if (VirtualRoot.IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(true); } } else // 不是作业 { if (VirtualRoot.IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(false); } // 如果是Debug模式且不是群控客户端则使用本地数据库初始化 bool useLocalDb = DevMode.IsDevMode && !VirtualRoot.IsMinerStudio; if (useLocalDb) { DoInit(isWork: false, callback: callback); } else { Logger.InfoDebugLine(nameof(GetAliyunServerJson)); GetAliyunServerJson((data) => { // 如果server.json未下载成功则不覆写本地server.json if (data != null && data.Length != 0) { Logger.InfoDebugLine($"{nameof(GetAliyunServerJson)}成功"); var serverJson = Encoding.UTF8.GetString(data); if (!string.IsNullOrEmpty(serverJson)) { SpecialPath.WriteServerJsonFile(serverJson); } RpcRoot.OfficialServer.GetJsonFileVersionAsync(EntryAssemblyInfo.ServerJsonFileName, serverState => { SetServerJsonVersion(serverState.JsonFileVersion); AppVersionChangedEvent.PublishIfNewVersion(serverState.MinerClientVersion); if (Math.Abs((long)Timestamp.GetTimestamp() - (long)serverState.Time) >= Timestamp.DesyncSeconds) { Write.UserWarn($"本机和服务器时间不同步,请调整,本地:{DateTime.Now.ToString()},服务器:{Timestamp.FromTimestamp(serverState.Time).ToString()}。此问题不影响挖矿。"); } }); } else { if (!File.Exists(SpecialPath.ServerJsonFileFullName)) { VirtualRoot.ThisLocalError(nameof(NTMinerRoot), "配置文件下载失败,这是第一次运行开源矿工,配置文件至少需要成功下载一次,请检查网络是否可用", OutEnum.Warn); } else { VirtualRoot.ThisLocalWarn(nameof(NTMinerRoot), "配置文件下载失败,使用最近一次成功下载的配置文件", OutEnum.Warn); } } DoInit(isWork, callback); }); #region 发生了用户活动时检查serverJson是否有新版本 VirtualRoot.AddEventPath <UserActionEvent>("发生了用户活动时检查serverJson是否有新版本", LogEnum.DevConsole, action: message => { RefreshServerJsonFile(); }, location: this.GetType()); #endregion } } VirtualRoot.ThisLocalInfo(nameof(NTMinerRoot), $"启动{VirtualRoot.AppName}"); }); }
public void Init(Action callback) { Task.Factory.StartNew(() => { bool isWork = Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase); if (isWork) { DoInit(isWork, callback); if (IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(true); } return; } else { if (IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(false); } } // 如果是Debug模式且不是群控客户端且不是作业则使用本地数据库初始化 if (DevMode.IsDebugMode && !VirtualRoot.IsMinerStudio) { DoInit(isWork: false, callback: callback); return; } Logger.InfoDebugLine("开始下载server.json"); SpecialPath.GetAliyunServerJson((data) => { // 如果server.json未下载成功则不覆写本地server.json if (data != null && data.Length != 0) { Logger.InfoDebugLine("GetAliyunServerJson下载成功"); var serverJson = Encoding.UTF8.GetString(data); if (!string.IsNullOrEmpty(serverJson)) { SpecialPath.WriteServerJsonFile(serverJson); } OfficialServer.GetJsonFileVersionAsync(AssemblyInfo.ServerJsonFileName, (serverJsonFileVersion, minerClientVersion) => { SetServerJsonVersion(serverJsonFileVersion); AppVersionChangedEvent.PublishIfNewVersion(minerClientVersion); }); } else { Logger.InfoDebugLine("GetAliyunServerJson下载失败"); } DoInit(isWork, callback); }); #region 发生了用户活动时检查serverJson是否有新版本 VirtualRoot.On <UserActionEvent>("发生了用户活动时检查serverJson是否有新版本", LogEnum.DevConsole, action: message => { OfficialServer.GetJsonFileVersionAsync(AssemblyInfo.ServerJsonFileName, (serverJsonFileVersion, minerClientVersion) => { AppVersionChangedEvent.PublishIfNewVersion(minerClientVersion); string localServerJsonFileVersion = GetServerJsonVersion(); if (!string.IsNullOrEmpty(serverJsonFileVersion) && localServerJsonFileVersion != serverJsonFileVersion) { SpecialPath.GetAliyunServerJson((data) => { Write.UserInfo($"server.json配置文件有新版本{localServerJsonFileVersion}->{serverJsonFileVersion}"); string rawJson = Encoding.UTF8.GetString(data); SpecialPath.WriteServerJsonFile(rawJson); SetServerJsonVersion(serverJsonFileVersion); ReInitServerJson(); bool isUseJson = !DevMode.IsDebugMode || VirtualRoot.IsMinerStudio; if (isUseJson) { // 作业模式下界面是禁用的,所以这里的初始化isWork必然是false ContextReInit(isWork: VirtualRoot.IsMinerStudio); Write.UserInfo("刷新完成"); } else { Write.UserInfo("不是使用的server.json,无需刷新"); } }); } else { Write.DevDebug("server.json没有新版本"); } }); }); #endregion }); // 因为这个操作大概需要200毫秒 Task.Factory.StartNew(() => { NVIDIAGpuSet.NvmlInit(); }); }
private CoinViewModels() { #if DEBUG Write.Stopwatch.Restart(); #endif if (Design.IsInDesignMode) { return; } VirtualRoot.On <ServerContextReInitedEvent>("ServerContext刷新后刷新VM内存", LogEnum.DevConsole, action: message => { _dicById.Clear(); Init(); }); VirtualRoot.On <ServerContextVmsReInitedEvent>("ServerContext的VM集刷新后刷新视图界面", LogEnum.DevConsole, action: message => { AllPropertyChanged(); }); On <CoinAddedEvent>("添加了币种后刷新VM内存", LogEnum.DevConsole, action: (message) => { _dicById.Add(message.Source.GetId(), new CoinViewModel(message.Source)); AppContext.Instance.MinerProfileVm.OnPropertyChanged(nameof(NTMiner.AppContext.Instance.MinerProfileVm.CoinVm)); AllPropertyChanged(); }); On <CoinRemovedEvent>("移除了币种后刷新VM内存", LogEnum.DevConsole, action: message => { _dicById.Remove(message.Source.GetId()); AppContext.Instance.MinerProfileVm.OnPropertyChanged(nameof(NTMiner.AppContext.Instance.MinerProfileVm.CoinVm)); AllPropertyChanged(); }); On <CoinUpdatedEvent>("更新了币种后刷新VM内存", LogEnum.DevConsole, action: message => { CoinViewModel coinVm = _dicById[message.Source.GetId()]; bool justAsDualCoin = coinVm.JustAsDualCoin; coinVm.Update(message.Source); coinVm.TestWalletVm.Address = message.Source.TestWallet; coinVm.OnPropertyChanged(nameof(coinVm.Wallets)); coinVm.OnPropertyChanged(nameof(coinVm.WalletItems)); if (AppContext.Instance.MinerProfileVm.CoinId == message.Source.GetId()) { AppContext.Instance.MinerProfileVm.OnPropertyChanged(nameof(NTMiner.AppContext.Instance.MinerProfileVm.CoinVm)); } CoinKernelViewModel coinKernelVm = AppContext.Instance.MinerProfileVm.CoinVm.CoinKernel; if (coinKernelVm != null && coinKernelVm.CoinKernelProfile.SelectedDualCoin != null && coinKernelVm.CoinKernelProfile.SelectedDualCoin.GetId() == message.Source.GetId()) { coinKernelVm.CoinKernelProfile.OnPropertyChanged(nameof(coinKernelVm.CoinKernelProfile.SelectedDualCoin)); } if (justAsDualCoin != coinVm.JustAsDualCoin) { OnPropertyChanged(nameof(MainCoins)); } }); On <CoinIconDownloadedEvent>("下载了币种图标后", LogEnum.DevConsole, action: message => { try { if (string.IsNullOrEmpty(message.Source.Icon)) { return; } string iconFileFullName = SpecialPath.GetIconFileFullName(message.Source); if (string.IsNullOrEmpty(iconFileFullName) || !File.Exists(iconFileFullName)) { return; } CoinViewModel coinVm; if (_dicById.TryGetValue(message.Source.GetId(), out coinVm)) { try { coinVm.IconImageSource = new Uri(iconFileFullName, UriKind.Absolute).ToString(); } catch (Exception e) { File.Delete(iconFileFullName); Logger.ErrorDebugLine(e); } } } catch (Exception e) { Logger.ErrorDebugLine(e); } }); Init(); #if DEBUG Write.DevTimeSpan($"耗时{Write.Stopwatch.ElapsedMilliseconds}毫秒 {this.GetType().Name}.ctor"); #endif }
private void DoInit(bool isWork, Action callback) { this.PackageDownloader = new PackageDownloader(this); this.AppSettingSet = new AppSettingSet(this); this.CalcConfigSet = new CalcConfigSet(this); ContextInit(isWork); // 打码支持内核品牌 if (!string.IsNullOrEmpty(CommandLineArgs.KernelBrand)) { if (SysDicItemSet.TryGetDicItem("KernelBrand", CommandLineArgs.KernelBrand, out ISysDicItem brandItem)) { #region KernelBrandId string brand = $"KernelBrandId{brandItem.GetId()}KernelBrandId"; byte[] data = Encoding.UTF8.GetBytes(brand); if (data.Length != KernelBrandRaw.Length) { throw new InvalidProgramException(); } byte[] source = File.ReadAllBytes(ClientId.AppFileFullName); int index = 0; for (int i = 0; i < source.Length - KernelBrandRaw.Length; i++) { int j = 0; for (; j < KernelBrandRaw.Length; j++) { if (source[i + j] != KernelBrandRaw[j]) { break; } } if (j == KernelBrandRaw.Length) { index = i; break; } } for (int i = index; i < index + data.Length; i++) { source[i] = data[i - index]; } string brandExeFullName = Path.Combine(Path.GetDirectoryName(ClientId.AppFileFullName), Path.GetFileNameWithoutExtension(ClientId.AppFileFullName) + $"_{CommandLineArgs.KernelBrand}.exe"); File.WriteAllBytes(brandExeFullName, source); #endregion Environment.Exit(0); return; } } this.UserSet = new UserSet(); this.KernelProfileSet = new KernelProfileSet(this); this.GpusSpeed = new GpusSpeed(this); this.CoinShareSet = new CoinShareSet(this); this.MineWorkSet = new MineWorkSet(this); this.MinerGroupSet = new MinerGroupSet(this); this.OverClockDataSet = new OverClockDataSet(this); this.ColumnsShowSet = new ColumnsShowSet(this); MineWorkData mineWorkData = null; if (isWork) { mineWorkData = LocalJson.MineWork; } this._minerProfile = new MinerProfile(this, mineWorkData); // 这几个注册表内部区分挖矿端和群控客户端 NTMinerRegistry.SetLocation(ClientId.AppFileFullName); NTMinerRegistry.SetArguments(string.Join(" ", CommandLineArgs.Args)); NTMinerRegistry.SetCurrentVersion(CurrentVersion.ToString()); NTMinerRegistry.SetCurrentVersionTag(CurrentVersionTag); #region 发生了用户活动时检查serverJson是否有新版本 VirtualRoot.On <UserActionEvent>("发生了用户活动时检查serverJson是否有新版本", LogEnum.DevConsole, action: message => { OfficialServer.GetJsonFileVersionAsync(AssemblyInfo.ServerJsonFileName, (jsonFileVersion) => { if (!string.IsNullOrEmpty(jsonFileVersion) && JsonFileVersion != jsonFileVersion) { SpecialPath.GetAliyunServerJson((data) => { Write.DevLine($"有新版本{JsonFileVersion}->{jsonFileVersion}"); string rawJson = Encoding.UTF8.GetString(data); SpecialPath.WriteServerJsonFile(rawJson); ReInitServerJson(); bool isUseJson = !DevMode.IsDebugMode || VirtualRoot.IsMinerStudio; if (isUseJson) { // 作业模式下界面是禁用的,所以这里的初始化isWork必然是false ContextReInit(isWork: false); Logger.InfoDebugLine("刷新完成"); } else { Write.DevLine("不是使用的json,无需刷新"); } JsonFileVersion = jsonFileVersion; }); } else { Write.DevLine("server.json没有新版本", ConsoleColor.Green); } }); }); #endregion callback?.Invoke(); }
public void Init(Action callback) { Task.Factory.StartNew(() => { bool isWork = Environment.GetCommandLineArgs().Contains("--work", StringComparer.OrdinalIgnoreCase); if (isWork) // 是作业 { DoInit(isWork, callback); if (VirtualRoot.IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(true); } } else // 不是作业 { if (VirtualRoot.IsMinerClient) { NTMinerRegistry.SetIsLastIsWork(false); } // 如果是Debug模式且不是群控客户端则使用本地数据库初始化 bool useLocalDb = DevMode.IsDebugMode && !VirtualRoot.IsMinerStudio; if (useLocalDb) { DoInit(isWork: false, callback: callback); } else { Logger.InfoDebugLine("开始下载server.json"); GetAliyunServerJson((data) => { // 如果server.json未下载成功则不覆写本地server.json if (data != null && data.Length != 0) { Logger.InfoDebugLine("GetAliyunServerJson下载成功"); var serverJson = Encoding.UTF8.GetString(data); if (!string.IsNullOrEmpty(serverJson)) { SpecialPath.WriteServerJsonFile(serverJson); } OfficialServer.GetJsonFileVersionAsync(MainAssemblyInfo.ServerJsonFileName, (serverJsonFileVersion, minerClientVersion) => { SetServerJsonVersion(serverJsonFileVersion); AppVersionChangedEvent.PublishIfNewVersion(minerClientVersion); }); } else { if (!File.Exists(SpecialPath.ServerJsonFileFullName)) { VirtualRoot.ThisWorkerError(nameof(NTMinerRoot), "配置文件下载失败,这是第一次运行开源矿工,配置文件至少需要成功下载一次,请检查网络是否可用", OutEnum.Warn); } else { VirtualRoot.ThisWorkerWarn(nameof(NTMinerRoot), "配置文件下载失败,使用最后一次成功下载的配置文件", OutEnum.Warn); } } DoInit(isWork, callback); }); #region 发生了用户活动时检查serverJson是否有新版本 VirtualRoot.BuildEventPath <UserActionEvent>("发生了用户活动时检查serverJson是否有新版本", LogEnum.DevConsole, action: message => { RefreshServerJsonFile(); }); #endregion } } VirtualRoot.ThisWorkerInfo(nameof(NTMinerRoot), "启动"); }); }