年度变更建库软件5.0版本
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

409 lines
18 KiB

6 months ago
using Fleck;
using KGIS.Framework.Core.Services;
using KGIS.Framework.Platform;
using KGIS.Framework.Platform.Interface;
using KGIS.Framework.Utils;
using KGIS.Framework.Utils.Helper;
using Kingo.Core.Authorize;
using Kingo.PluginServiceInterface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Management;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ServiceProcess;
using System.Threading;
using System.Windows.Forms;
using UIShell.OSGi;
namespace Kingo.BuildDB
{
static class Program
{
internal delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern int GetWindowThreadProcessId(IntPtr hWnd, out IntPtr lpdwProcessId);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
internal static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public static bool v = false;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] agrs)
{
string SystemName = "数 据";
if (agrs.Length ==0)
agrs = new string[] { "NDBG" };
if (agrs.Length > 0)
{
switch (agrs[0])
{
case "DTBJK":
Platform.Instance.SystemType = SystemTypeEnum.DTBJK;
SystemName = "单 图 斑";
break;
case "RCBG":
Platform.Instance.SystemType = SystemTypeEnum.RCBGJK;
SystemName = "日 常 变 更";
break;
case "NDBG":
Platform.Instance.SystemType = SystemTypeEnum.NDBGJK;
SystemName = "年 度 变 更";
break;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(platform.Instance);
string cfgFilePath = System.IO.Path.Combine(SysAppPath.GetConfigPath(), "LayersCfg.xml");
dynamic Cfg = SerializeAPI.DeserializeToObject2<dynamic>(cfgFilePath);
Mutex instance = new Mutex(true, $"{Platform.Instance.SystemType}_30", out bool createdNew);
//if (createdNew)
//{
//}
//else
//{
//MessageHelper.ShowTips("已经打开该应用程序,请先关闭!");
Process curr = Process.GetCurrentProcess();
List<Process> process = Process.GetProcessesByName(curr.ProcessName).ToList();//.
//Where(p => p.Id != curr.Id && p.GetCommandLine().Contains($"{Platform.Instance.SystemType}_30")).ToList();
if (process != null && process.Count > 0)
{
foreach (Process item in process)
{
if (item.Id == curr.Id) continue;
ShowWindow(item.MainWindowHandle, 1);
SetForegroundWindow(item.MainWindowHandle);
Application.Exit();
return;
}
}
//}
try
{
GdalConfiguration.ConfigureGdal();
GdalConfiguration.ConfigureOgr();
}
catch (Exception ex)
{
LogAPI.Debug("Gdal注册失败!");
LogAPI.Debug(ex);
}
try
{
ServiceController sc = new ServiceController("ArcGIS License Manager");
if (!KGIS.Framework.AE.AELicense.AoInit(true) || sc.Status == ServiceControllerStatus.Stopped)
{
MessageHelper.Show("ArcGIS授权服务未启动或已到期,请检查ArcGIS授权!");
return;
}
}
catch (Exception EX)
{
MessageHelper.Show("ArcGIS授权服务未启动或已到期,请检查ArcGIS授权!");
LogAPI.Debug(EX.Message);
return;
}
using (var bundleRuntime = new BundleRuntime())
{
#region
int loadedBundleCount = 0;
Action<bool> action = null;
IAsyncResult asyncResult = null;
IPluginStart startPlugin = null;
List<IBundle> bundles = new List<IBundle>();
//插件状态发生改变事件,用来加载启动插件的进度
EventHandler<BundleStateChangedEventArgs> bundleStateChangeEventHandler = (sender, e) =>
{
if (e.CurrentState != BundleState.Starting || e.Bundle.Name == "SystemBundle")
return;
if (startPlugin == null)
startPlugin = bundleRuntime.GetFirstOrDefaultService<IPluginStart>();
string[] Name_ID = e.Bundle.Name.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
if (startPlugin != null)
{
startPlugin.SetTitle(string.Format(ConfigurationManager.AppSettings.Get("APPName"), SystemName));
//if (args != null && args.Length > 0)
//{
//startPlugin.SetTitle(args[0]);
startPlugin.SetSystemType(SystemTypeEnum.NDBGJK.ToString());
//if (Name_ID.Length == 2)
//{
// if (!Name_ID[1].Contains(SystemTypeEnum.NDBGJK.ToString()))
// {
// bundles.Add(e.Bundle);
// //e.Bundle.Stop(BundleStopOptions.Transient);
// }
//}
//else
//{
// bundles.Add(e.Bundle);
//}
//}
if (!startPlugin.IsShow)
{
action = (ar) =>
{
if (ar)
{
startPlugin.ShowDialog();
}
else
{
}
//isWelcomeShow = true;
};
asyncResult = action.BeginInvoke(true, null, null);
//等待启动界面打开
while (!startPlugin.IsShow)
{
Thread.Sleep(50);
}
loadedBundleCount++;
startPlugin.SetPercent(loadedBundleCount, bundleRuntime.Framework.Bundles.Count - 1, Name_ID[0]);
}
else
{
loadedBundleCount++;
startPlugin.SetPercent(loadedBundleCount, bundleRuntime.Framework.Bundles.Count - 1, Name_ID[0]);
}
}
else
{
LogAPI.Debug("000000");
}
//e.Bundle.BundleID
};
#if DEBUG
Validity(bundleRuntime);
#else
Validity(bundleRuntime);
#endif
if (!v)
return;
//if (RunIDService2.Instance.Codes.Contains("210781"))
//{
// string ring = SysConfigsOprator.GetAppsetingValueByKey("210781");
// //MessageHelper.Show(RunIDService2.Instance.Secretkey);
// string ring2 = Decrypt(ring, RunIDService2.Instance.Secretkey);
// RunIDService2.Instance.Area = ring2;
//}
//if (string.IsNullOrWhiteSpace(RunIDService2.Instance.Area))
//{
// string ring = SysConfigsOprator.GetAppsetingValueByKey("Ring");
// ring = Decrypt(ring, RunIDService2.Instance.Secretkey);
// ring = ring.Replace("POLYGON((", "").Replace("))", "");
// ring = ring.Replace(',', ';');
// ring = ring.Replace(' ', ',');
// RunIDService2.Instance.Area = ring;
//}
//注册插件状态事件
bundleRuntime.Framework.EventManager.AddBundleEventListener(bundleStateChangeEventHandler, true);
//开始启动插件
bundleRuntime.Start();
//取消插件状态的事件
bundleRuntime.Framework.EventManager.RemoveBundleEventListener(bundleStateChangeEventHandler, true);
#endregion
//foreach (IBundle item in bundles)
//{
// LogAPI.Debug("Del:" + item.Name);
// item.Stop(BundleStopOptions.Transient);
//}
var platform = bundleRuntime.GetFirstOrDefaultService<IPlatformService>();
if (platform != null)
{
//platform.InitService();
Platform.Instance.Open();
platform.InitUI(null);
if (startPlugin != null)
startPlugin.CloseForm();
ExtensionShowWindow.MainWinForm = platform.Instance;
Platform.Instance.OutputMsg("系统初始化完成。");
Platform.Instance.UpdateStateBar("系统初始化完成");
//if (Platform.Instance.MapsService != null)
//{
//}
//else
//{
// LogAPI.Debug("软件启动失败,可能的原因:");
// LogAPI.Debug("ArcGIS授权服务未启动或已到期,请检查ArcGIS授权!");
//}
//GdalConfiguration.ConfigureGdal();
//GdalConfiguration.ConfigureOgr();
WebsocketHelper.Instance.ConnectionCallback = (s) =>
{
WebsocketHelper.Instance.Conn = s as IWebSocketConnection;
{
(s as IWebSocketConnection).OnOpen = () => { };
}
};
WebsocketHelper.Instance.Start(7080);
Application.Run(platform.Instance);
RunIDService.Dispose();
}
else
{
LogAPI.Debug("IPlatformService接口获取失败!");
}
}
}
public static string Decrypt(string content, string strKey)
{
string result = string.Empty;
try
{
if (string.IsNullOrWhiteSpace(content))
return string.Empty;
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
{
Key = System.Text.Encoding.UTF8.GetBytes(strKey),
Mode = System.Security.Cryptography.CipherMode.ECB,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
//将base64字符串转换成byte数组
byte[] contentArray = Convert.FromBase64String(content);
//创建解密转换器
System.Security.Cryptography.ICryptoTransform cDecryptTransform = rm.CreateDecryptor();
//对byte数组进行解密转换
Byte[] DecryptArray = cDecryptTransform.TransformFinalBlock(contentArray, 0, contentArray.Length);
//将解密后的byte[]转换成字符串(base64)
string strDecrypt = System.Text.Encoding.UTF8.GetString(DecryptArray);
//将base64字符串转换成8位无符号整数数组
byte[] base64ToByte = Convert.FromBase64String(strDecrypt);
//string s = Encoding.UTF8.GetString(base64ToByte);
//将8位无符号整数数组转换成等效字符串,并返回
return System.Text.Encoding.UTF8.GetString(base64ToByte);
}
catch (Exception ex)
{
throw new Exception("解密工程文件异常:" + ex.Message);
}
finally
{
GC.Collect();
}
}
private static void Validity(object obj)
{
try
{
//判断授权方式
var type = ConfigurationManager.AppSettings.Get("LicenseType");
if (type == "F")//本地授权
{
Attribute guid_attr = Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(GuidAttribute));
string guid = ((GuidAttribute)guid_attr).Value;
try
{
string[] licpath = System.IO.Directory.GetFiles(System.Environment.CurrentDirectory, "*.lic", System.IO.SearchOption.TopDirectoryOnly);
if (licpath.Length == 0)
{
MessageHelper.ShowError("授权校验失败!");
}
if (licpath.Length == 0 || !RunIDService2.Instance.Verify(AuthorizeType., "KGIS_SJJK", "400", "4x", "", "10eaa61"))
{
KGIS.PlatformPlugin.Views.License.LicenseView lic = new KGIS.PlatformPlugin.Views.License.LicenseView(obj, "KGIS_SJJK", "400", "4x");
lic.Code = "10eaa61";
int num = -1;
//lic.DocValidity = Validity;
DialogResult dialog = lic.ShowDialog();
if (dialog != DialogResult.Yes && dialog != DialogResult.OK)
{
v = false;
return;
}
}
else
{
v = true;
return;
}
}
catch (Exception ex)
{
MessageHelper.Show(ex.Message);
LogAPI.Debug(ex);
KGIS.PlatformPlugin.Views.License.LicenseView lic = new KGIS.PlatformPlugin.Views.License.LicenseView(obj, "KGIS_SJJK", "400", "4x");
lic.Code = "10eaa61";
int num = -1;
//lic.DocValidity = Validity;
DialogResult dialog = lic.ShowDialog();
if (dialog != DialogResult.Yes || dialog != DialogResult.OK)
{
v = false;
return;
}
}
}
else if (type == "D")
{
if (!RunIDService2.Instance.Verify("BGDCPRO", "300", "3x"))
{
KGIS.PlatformPlugin.Views.License.LicenseView lic = new KGIS.PlatformPlugin.Views.License.LicenseView(obj, "KGIS_SJJK", "400", "4x");
DialogResult dialog = lic.ShowDialog();
if (dialog != DialogResult.Yes && dialog != DialogResult.OK)
{
v = false;
return;
}
}
}
else
{
KGIS.PlatformPlugin.Views.License.LicenseView lic = new KGIS.PlatformPlugin.Views.License.LicenseView(obj, "KGIS_SJJK", "400", "4x");
int num = -1;
lic.DocValidity = Validity;
DialogResult dialog = lic.ShowDialog();
if (dialog != DialogResult.Yes || dialog != DialogResult.OK)
{
v = false;
return;
}
}
v = true;
}
catch (Exception ex)
{
LogAPI.Debug(ex);
}
}
private static string GetCommandLine(this Process process)
{
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
using (ManagementObjectCollection objects = searcher.Get())
{
return objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString();
}
}
}
}