-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
508 lines (435 loc) · 20.8 KB
/
Program.cs
File metadata and controls
508 lines (435 loc) · 20.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//
////////////////////////////////////////////////////////////////
//
/**************************************************************
** __ __
** __/_/__________________________________________\_\__
** __|_ |__
** (___O) Ouslan, Inc. (O___)
**(_____O) ainetstudio.com (O_____)
**(_____O) Author: Bill SerGio, Infomercial King™ (O_____)
** (__O) (O__)
** |___________________________________________________|
**
****************************************************************/
/*
* (C) Copyright 2024-2025 Ouslan,Inc, All Rights Reserved Worldwide.
* software-rus.com
* tvmogul1@yahoo.com
*
*/
using AiNetStudio.DataAccess;
using AiNetStudio.WinGui.Dialogs;
using AiNetStudio.WinGui.Forms;
using Serilog;
using Serilog.Events;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace AiNetStudio
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
// ---- Serilog bootstrap ----
var exeDir = Path.GetDirectoryName(Application.ExecutablePath) ?? AppContext.BaseDirectory;
var logDir = Path.Combine(exeDir, "AAALogs");
Directory.CreateDirectory(logDir);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.WithProcessId()
.Enrich.WithThreadId()
.Enrich.FromLogContext()
.WriteTo.Debug() // VS Output window
.WriteTo.File(
Path.Combine(logDir, "app-.log"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 7,
restrictedToMinimumLevel: LogEventLevel.Information,
shared: true)
.CreateLogger();
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
Log.Fatal(e.ExceptionObject as Exception, "Unhandled exception");
Application.ThreadException += (s, e) =>
Log.Error(e.Exception, "WinForms thread exception");
// Single-instance mutex to prevent multiple app instances
bool createdNew = false;
using var _singleInstanceMutex = new Mutex(initiallyOwned: true, name: @"Global\AiNetStudio_SingleInstance", out createdNew);
if (!createdNew)
{
Log.Information("Another instance is already running. Exiting.");
Log.CloseAndFlush();
return;
}
try
{
Log.Information("AiNetStudio starting…");
// ---- Copy required files if they don't already exist ----
// First-run seeding: only perform copy if the app data root does NOT exist yet
var localApp = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var appRoot = Path.Combine(localApp, "AiNetStudio");
var isFirstRun = !Directory.Exists(appRoot);
if (isFirstRun)
{
Directory.CreateDirectory(appRoot); // ensure root exists for writable folders
try
{
var pm = new PathManager();
var dbPath = Path.Combine(pm.GetWritableFolder("Databases"), "rssfeeds.aidb");
var dir = Path.GetDirectoryName(dbPath)!;
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
// UPDATED: Resolve Libs from multiple possible locations (bin output, project root, parent folders)
var srcLibs = ResolveLibsFolder(exeDir);
if (srcLibs == null)
{
var msgNoLibs = $"Unable to locate 'Libs' folder.\nSearched around:\n{exeDir}\n\nEnsure a 'Libs' folder exists with required files (techarchive.aidb, rssfeeds.aidb).";
Log.Error(msgNoLibs);
ErrorDialog.Show("Startup Copy Error", msgNoLibs);
return;
}
Log.Information("Using Libs source at: {SrcLibs}", srcLibs);
var dstLibs = pm.GetWritableFolder("Libs");
var dstDatabases = pm.GetWritableFolder("Databases");
if (!Directory.Exists(dstLibs)) Directory.CreateDirectory(dstLibs);
if (!Directory.Exists(dstDatabases)) Directory.CreateDirectory(dstDatabases);
var srcExport = Path.Combine(srcLibs, "feeds_export.sql");
var dstExport = Path.Combine(dstLibs, "feeds_export.sql");
if (File.Exists(srcExport) && !File.Exists(dstExport)) File.Copy(srcExport, dstExport, overwrite: false);
var srcSubset = Path.Combine(srcLibs, "feeds_subset.csv");
var dstSubset = Path.Combine(dstLibs, "feeds_subset.csv");
if (File.Exists(srcSubset) && !File.Exists(dstSubset)) File.Copy(srcSubset, dstSubset, overwrite: false);
var srcAidb = Path.Combine(srcLibs, "rssfeeds.aidb");
var dstAidb = Path.Combine(dstDatabases, "rssfeeds.aidb");
if (File.Exists(srcAidb) && !File.Exists(dstAidb)) File.Copy(srcAidb, dstAidb, overwrite: false);
var srcAidb2 = Path.Combine(srcLibs, "techarchive.aidb");
var dstAidb2 = Path.Combine(dstDatabases, "techarchive.aidb");
if (File.Exists(srcAidb2) && !File.Exists(dstAidb2)) File.Copy(srcAidb2, dstAidb2, overwrite: false);
// ////////////////////////////////////////////////////////////
var srcPdfs = Path.Combine(srcLibs, "Library");
var dstPdfs = pm.GetWritableFolder("Library");
Directory.CreateDirectory(dstPdfs);
// Copy any missing PDFs (idempotent)
if (Directory.Exists(srcPdfs))
{
foreach (var srcFile in Directory.EnumerateFiles(srcPdfs, "*.pdf", SearchOption.TopDirectoryOnly))
{
var fileName = Path.GetFileName(srcFile);
var dstFile = Path.Combine(dstPdfs, fileName);
if (!File.Exists(dstFile))
{
File.Copy(srcFile, dstFile, overwrite: false);
}
}
}
else
{
Log.Error("Source PDF library folder not found at: {SrcPdfs}", srcPdfs);
}
////////////////////////////////////////////////////////////
// Final verification: ensure both DBs exist after copy attempt
var missing = new StringBuilder();
if (!File.Exists(Path.Combine(dstDatabases, "rssfeeds.aidb"))) missing.AppendLine("rssfeeds.aidb");
if (!File.Exists(Path.Combine(dstDatabases, "techarchive.aidb"))) missing.AppendLine("techarchive.aidb");
if (missing.Length > 0)
{
var msg = $"Database file(s) missing after copy attempt in:\n{dstDatabases}\n\nMissing:\n{missing}\n" +
$"Source Libs: {srcLibs}";
Log.Error(msg);
ErrorDialog.Show("Startup Copy Error", msg);
return;
}
Log.Information("First-run seeding complete.");
}
catch (Exception copyEx)
{
Log.Error(copyEx, "Error copying seed files.");
ErrorDialog.Show("Startup Copy Error", copyEx.ToString());
return;
}
}
else
{
Log.Information("App data folder already exists; skipping first-run seeding.");
}
// ---------------------------------------------------------
// COPY-ONLY MODE: PDFs are handled above; importer disabled.
// (Removed asynchronous importer block intentionally.)
ApplicationConfiguration.Initialize();
//Application.Run(new WinGUIMain());
using (SkinDlg splash = new SkinDlg())
{
if (splash.ShowDialog() == DialogResult.OK)
{
Application.Run(new WinGUIMain());
}
else
{
//return;
Application.Run(new WinGUIMain());
}
}
Log.Information("AiNetStudio exited normally.");
}
catch (Exception ex)
{
Log.Fatal(ex, "Fatal app crash");
throw;
}
finally
{
Log.CloseAndFlush();
}
}
/// <summary>
/// Locate the Libs folder by checking:
/// 1) PathManager installation folder
/// 2) exeDir\Libs
/// 3) Walking up parent directories for a 'Libs' that contains techarchive.aidb
/// Returns null if not found.
/// </summary>
private static string? ResolveLibsFolder(string exeDir)
{
try
{
// 1) PathManager-provided installation folder
try
{
var pm = new PathManager();
var pmLibs = pm.GetInstallationFolder("Libs");
if (Directory.Exists(pmLibs) &&
File.Exists(Path.Combine(pmLibs, "techarchive.aidb")))
return pmLibs;
}
catch { /* ignore if PathManager not ready */ }
// 2) exeDir\Libs
var directLibs = Path.Combine(exeDir, "Libs");
if (Directory.Exists(directLibs) &&
File.Exists(Path.Combine(directLibs, "techarchive.aidb")))
return directLibs;
// 3) Walk up parent directories to find a Libs containing the DB
var cursor = new DirectoryInfo(exeDir);
int hops = 0;
while (cursor != null && hops < 6) // up to 6 levels up should cover common dev layouts
{
var probe = Path.Combine(cursor.FullName, "Libs");
if (Directory.Exists(probe) &&
File.Exists(Path.Combine(probe, "techarchive.aidb")))
return probe;
cursor = cursor.Parent;
hops++;
}
}
catch
{
// swallow and let caller handle null
}
return null;
}
}
}
//using AiNetStudio.DataAccess;
//using AiNetStudio.WinGui.Dialogs;
//using AiNetStudio.WinGui.Forms;
//using Serilog;
//using Serilog.Events;
//using System;
//using System.Diagnostics;
//using System.IO;
//using System.Text;
//using System.Threading;
//using System.Windows.Forms;
//namespace AiNetStudio
//{
// internal static class Program
// {
// /// <summary>
// /// The main entry point for the application.
// /// </summary>
// [STAThread]
// static void Main()
// {
// // To customize application configuration such as set high DPI settings or default font,
// // see https://aka.ms/applicationconfiguration.
// // ---- Serilog bootstrap ----
// var exeDir = Path.GetDirectoryName(Application.ExecutablePath) ?? AppContext.BaseDirectory;
// var logDir = Path.Combine(exeDir, "AAALogs");
// Directory.CreateDirectory(logDir);
// Log.Logger = new LoggerConfiguration()
// .MinimumLevel.Debug()
// .MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
// .Enrich.WithProcessId()
// .Enrich.WithThreadId()
// .Enrich.FromLogContext()
// .WriteTo.Debug() // VS Output window
// .WriteTo.File(
// Path.Combine(logDir, "app-.log"),
// rollingInterval: RollingInterval.Day,
// retainedFileCountLimit: 7,
// restrictedToMinimumLevel: LogEventLevel.Information,
// shared: true)
// .CreateLogger();
// AppDomain.CurrentDomain.UnhandledException += (s, e) =>
// Log.Fatal(e.ExceptionObject as Exception, "Unhandled exception");
// Application.ThreadException += (s, e) =>
// Log.Error(e.Exception, "WinForms thread exception");
// // Single-instance mutex to prevent multiple app instances
// bool createdNew = false;
// using var _singleInstanceMutex = new Mutex(initiallyOwned: true, name: @"Global\AiNetStudio_SingleInstance", out createdNew);
// if (!createdNew)
// {
// Log.Information("Another instance is already running. Exiting.");
// Log.CloseAndFlush();
// return;
// }
// try
// {
// Log.Information("AiNetStudio starting…");
// // ---- Copy required files if they don't already exist ----
// try
// {
// var pm = new PathManager();
// var dbPath = Path.Combine(pm.GetWritableFolder("Databases"), "rssfeeds.aidb");
// var dir = Path.GetDirectoryName(dbPath)!;
// if (!Directory.Exists(dir))
// Directory.CreateDirectory(dir);
// var srcLibs = Path.Combine(exeDir, "Libs");
// var dstLibs = pm.GetWritableFolder("Libs");
// var dstDatabases = pm.GetWritableFolder("Databases");
// if (!Directory.Exists(dstLibs)) Directory.CreateDirectory(dstLibs);
// if (!Directory.Exists(dstDatabases)) Directory.CreateDirectory(dstDatabases);
// var srcExport = Path.Combine(srcLibs, "feeds_export.sql");
// var dstExport = Path.Combine(dstLibs, "feeds_export.sql");
// if (File.Exists(srcExport) && !File.Exists(dstExport)) File.Copy(srcExport, dstExport, overwrite: false);
// var srcSubset = Path.Combine(srcLibs, "feeds_subset.csv");
// var dstSubset = Path.Combine(dstLibs, "feeds_subset.csv");
// if (File.Exists(srcSubset) && !File.Exists(dstSubset)) File.Copy(srcSubset, dstSubset, overwrite: false);
// var srcAidb = Path.Combine(srcLibs, "rssfeeds.aidb");
// var dstAidb = Path.Combine(dstDatabases, "rssfeeds.aidb");
// if (File.Exists(srcAidb) && !File.Exists(dstAidb)) File.Copy(srcAidb, dstAidb, overwrite: false);
// var srcAidb2 = Path.Combine(srcLibs, "techarchive.aidb");
// var dstAidb2 = Path.Combine(dstDatabases, "techarchive.aidb");
// if (File.Exists(srcAidb2) && !File.Exists(dstAidb2)) File.Copy(srcAidb2, dstAidb2, overwrite: false);
// // ////////////////////////////////////////////////////////////
// var srcPdfs = Path.Combine(srcLibs, "Library");
// var dstPdfs = pm.GetWritableFolder("Library");
// Directory.CreateDirectory(dstPdfs);
// // Copy any missing PDFs (idempotent)
// foreach (var srcFile in Directory.EnumerateFiles(srcPdfs, "*.pdf", SearchOption.TopDirectoryOnly))
// {
// var fileName = Path.GetFileName(srcFile);
// var dstFile = Path.Combine(dstPdfs, fileName);
// if (!File.Exists(dstFile))
// {
// File.Copy(srcFile, dstFile, overwrite: false);
// }
// }
// ////////////////////////////////////////////////////////////
// //var q = pm.GetWritableFolder("Databases");
// //var dbPDFs = Path.Combine(q, "techarchive.aidb");
// //DbInitializer.EnsurePatentsSchema(dbPDFs);
// }
// catch { /* ignore copy errors */ }
// // ---------------------------------------------------------
// // Run import on startup (wrap in Task.
// // Run so we can call async code)
// Task.Run(async () =>
// {
// var pm2 = new PathManager();
// var dbDir = pm2.GetWritableFolder("Databases");
// Directory.CreateDirectory(dbDir);
// var dbPDFs = Path.Combine(dbDir, "techarchive.aidb");
// string sourceFolder = Path.Combine(pm2.GetInstallationFolder("Libs"), "Library");
// string appDataRoot = pm2.GetWritableFolder("");
// MessageBox.Show($"Import from:\n{sourceFolder}\n\nDB:\n{dbPDFs}\n\nLib root:\n{appDataRoot}", "Importer Paths");
// try
// {
// int imported = await PdfBulkImporter.ImportFolderAsync(
// sourceFolder,
// appDataRoot,
// dbPDFs,
// new Progress<(int done, int total, string file)>(p =>
// {
// Debug.WriteLine($"[{p.done}/{p.total}] {p.file}");
// })
// );
// MessageBox.Show($"Imported {imported} PDFs.", "Importer");
// }
// catch (Exception ex)
// {
// var msg = $"IMPORT FAILED\n\nMessage:\n{ex.Message}\n\nStackTrace:\n{ex.StackTrace}";
// ErrorDialog.Show("Importer Error", msg);
// }
// }).Wait();
// ApplicationConfiguration.Initialize();
// //Application.Run(new WinGUIMain());
// using (SkinDlg splash = new SkinDlg())
// {
// if (splash.ShowDialog() == DialogResult.OK)
// {
// Application.Run(new WinGUIMain());
// }
// else
// {
// //return;
// Application.Run(new WinGUIMain());
// }
// }
// Log.Information("AiNetStudio exited normally.");
// }
// catch (Exception ex)
// {
// Log.Fatal(ex, "Fatal app crash");
// throw;
// }
// finally
// {
// Log.CloseAndFlush();
// }
// }
// }
//}
//public static class ErrorDialog
//{
// public static void Show(string title, string message)
// {
// Form f = new Form
// {
// Text = title,
// Width = 700,
// Height = 500,
// StartPosition = FormStartPosition.CenterScreen
// };
// var tb = new TextBox
// {
// Multiline = true,
// ReadOnly = true,
// ScrollBars = ScrollBars.Both,
// Dock = DockStyle.Fill,
// Font = new Font("Consolas", 10),
// Text = message,
// WordWrap = false
// };
// f.Controls.Add(tb);
// var ok = new Button
// {
// Text = "OK",
// Dock = DockStyle.Bottom,
// DialogResult = DialogResult.OK
// };
// f.Controls.Add(ok);
// f.AcceptButton = ok;
// f.ShowDialog();
// }
//}