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 KIngo.DataDTBJK;
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.DataDTBJK
{
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;
///
/// 应用程序的主入口点。
///
[STAThread]
static void Main(string[] agrs)
{
string SystemName = "数 据";
if (agrs.Length > 0)
{
switch (agrs[0])
{
case "DTBJK":
Platform.Instance.SystemType = SystemTypeEnum.DTBJK;
SystemName = "单 图 斑 建 库";
break;
case "YCLJK":
Platform.Instance.SystemType = SystemTypeEnum.YCLJK;
SystemName = "预 处 理";
break;
case "WYZS":
Platform.Instance.SystemType = SystemTypeEnum.WYZS;
SystemName = "质 检";
break;
}
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
GdalConfiguration.ConfigureGdal();
GdalConfiguration.ConfigureOgr();
//Application.Run(platform.Instance);
Mutex instance = new Mutex(true, $"{Platform.Instance.SystemType}_30", out bool createdNew);
if (createdNew)
{
}
else
{
//MessageHelper.ShowTips("已经打开该应用程序,请先关闭!");
Process curr = Process.GetCurrentProcess();
List process = Process.GetProcessesByName(curr.ProcessName).ToList();
if (process != null && process.Count > 0)
{
foreach (Process item in process)
{
ShowWindow(item.MainWindowHandle, 1);
SetForegroundWindow(item.MainWindowHandle);
break;
}
}
Application.Exit();
return;
}
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())
{
IBundle bundle = null;
#region
int loadedBundleCount = 0;
Action action = null;
IAsyncResult asyncResult = null;
IPluginStart startPlugin = null;
//List bundles = new List();
//插件状态发生改变事件,用来加载启动插件的进度
EventHandler bundleStateChangeEventHandler = (sender, e) =>
{
if (e.CurrentState != BundleState.Starting || e.Bundle.Name == "SystemBundle")
return;
if (startPlugin == null)
startPlugin = bundleRuntime.GetFirstOrDefaultService();
//string[] Name_ID = e.Bundle.Name.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
if (startPlugin != null)
{
startPlugin.SetTitle(string.Format(ConfigurationManager.AppSettings.Get("APPName"), SystemName));
startPlugin.SetSystemType(SystemTypeEnum.DTBJK.ToString());
//if (Name_ID.Length == 2)
//{
// if (!Name_ID[1].Contains(SystemTypeEnum.DTBJK.ToString()))
// {
// bundles.Add(e.Bundle);
// }
//}
if (!startPlugin.IsShow)
{
action = (ar) =>
{
if (ar)
{
startPlugin.ShowDialog();
}
};
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]);
}
}
};
#if DEBUG
Validity(bundleRuntime);
#else
Validity(bundleRuntime);
#endif
//注册插件状态事件
bundleRuntime.Framework.EventManager.AddBundleEventListener(bundleStateChangeEventHandler, true);
//开始启动插件
bundleRuntime.Start();
//取消插件状态的事件
bundleRuntime.Framework.EventManager.RemoveBundleEventListener(bundleStateChangeEventHandler, true);
#endregion
#region 判断是否存在预处理模块
//var bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DTBYCL"));
//if (bundle != null)
//{
// UCSystemSelect ucSystemSelect = new UCSystemSelect
// {
// Topmost = true
// };
// if (ucSystemSelect.ShowDialog() != true)
// {
// return;
// }
//}
#endregion
#region 判断是否需要登录界面
if (KGIS.Framework.Utils.SysConfigsOprator.GetAppsetingValueByKey("IsNeedLogin").Equals("true"))
{
UCUserLogin uCUserLogin = new UCUserLogin()
{
Topmost = true
};
if (uCUserLogin.ShowDialog() != true)
{
return;
}
}
#endregion
switch (Platform.Instance.SystemType)
{
case SystemTypeEnum.YCLJK:
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DTBJK"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DTBZJ"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DLTB_IDG"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DataLoad"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
break;
case SystemTypeEnum.DTBJK:
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DTBYCL"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DTBZJ"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DLTB_IDG"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DataLoad"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
break;
case SystemTypeEnum.WYZS://审核
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DTBYCL"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DTBJK"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DLTB_IDG"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.DataLoad"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
bundle = bundleRuntime.Framework.Bundles.Find(x => x.SymbolicName.Equals("Kingo.Plugin.EngineEditor"));
if (bundle != null)
{
bundle.Stop(BundleStopOptions.Transient);
}
break;
}
var platform = bundleRuntime.GetFirstOrDefaultService();
if (platform != null)
{
Platform.Instance.Open();
platform.InitUI(null);
if (startPlugin != null)
startPlugin.CloseForm();
ExtensionShowWindow.MainWinForm = platform.Instance;
Platform.Instance.OutputMsg("系统初始化完成。");
Platform.Instance.UpdateStateBar("系统初始化完成");
LogAPI.Debug("系统初始化完成");
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接口获取失败!");
}
}
}
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().SingleOrDefault()?["CommandLine"]?.ToString();
}
}
}
}