@@ -45,6 +45,7 @@ private void Update()
4545 {
4646 UITreeView . Item parent = DebugScreen . AddContentItem ( null , "bugreport" , "Bug report" , null , null ) ;
4747 DebugScreen . AddContentItem ( parent , "createreport" , "Create report" , null , CreateReportFromDebugScreen ) ;
48+ DebugScreen . AddContentItem ( parent , "addscreenshot" , "Add screenshot to last report" , null , AddScreenshotToLastReport ) ;
4849 DebugScreen . AddContentItem ( parent , "uploadreport" , "Upload last report" , null , UploadLastReport ) ;
4950 DebugScreen . AddContentItem ( parent , "copylinktoclipoard" , "Copy last upload link to clipboard" , null , CopyLastLinkToClipboard ) ;
5051 DebugScreen . AddContentItem ( parent , "openreport" , "Open last report" , null , OpenReportFromDebugScreen ) ;
@@ -58,7 +59,7 @@ private void Update()
5859
5960 private static string lastReportPath = string . Empty ;
6061 private static string lastReportFolderPath = string . Empty ;
61- private static string lastReportUploadURI = string . Empty ;
62+ public static string lastReportUploadURI = string . Empty ;
6263
6364 private static void CreateReportFromDebugScreen ( )
6465 {
@@ -112,68 +113,30 @@ private static void UploadLastReport()
112113
113114 private static IEnumerator Upload ( )
114115 {
115- // list of potential services here : https://gist.github.com/Prajjwal/2226c6a96d1d72abc713e889160a9f81
116+ byte [ ] fileData = File . ReadAllBytes ( lastReportPath ) ;
117+ string fileName = Path . GetFileName ( lastReportPath ) ;
116118
117- WWWForm form = new WWWForm ( ) ;
118- form . AddBinaryData ( "file" , File . ReadAllBytes ( lastReportPath ) , Path . GetFileName ( lastReportPath ) ) ;
119+ UploadService primaryService = new UploadServiceOshi ( fileData , fileName ) ;
120+ yield return HighLogic . fetch . StartCoroutine ( primaryService . Send ( ) ) ;
119121
120- using ( UnityWebRequest www = UnityWebRequest . Post ( "https://0x0.st" , form ) )
122+ if ( primaryService . Success )
121123 {
122- yield return www . SendWebRequest ( ) ;
123-
124- try
125- {
126- if ( www . isNetworkError || www . isHttpError )
127- {
128- Lib . Log ( www . error , Lib . LogLevel . Warning ) ;
129- throw new Exception ( ) ;
130- }
131- else
132- {
133- lastReportUploadURI = www . downloadHandler . text ;
134- ScreenMessages . PostScreenMessage (
135- $ "Upload complete for <color=#FF8000>{ Path . GetFileName ( lastReportPath ) } </color> to the 0x0.st sharing service (~1 year retention)",
136- 10f , ScreenMessageStyle . UPPER_CENTER , true ) ;
137- CopyLastLinkToClipboard ( ) ;
138- yield break ;
139- }
140- }
141- catch ( Exception )
142- {
143- ScreenMessages . PostScreenMessage ( $ "Upload failed to 0x0.st", 3f , ScreenMessageStyle . UPPER_CENTER , Color . red ) ;
144- }
124+ yield break ;
145125 }
146126
147- using ( UnityWebRequest www = UnityWebRequest . Post ( "https://file.io" , form ) )
148- {
149- yield return www . SendWebRequest ( ) ;
127+ UploadService secondaryService = new UploadService0x0 ( fileData , fileName ) ;
128+ yield return HighLogic . fetch . StartCoroutine ( secondaryService . Send ( ) ) ;
150129
151- try
152- {
153- if ( www . isNetworkError || www . isHttpError )
154- {
155- Lib . Log ( www . error , Lib . LogLevel . Warning ) ;
156- throw new Exception ( ) ;
157- }
158- else
159- {
160- FileUploadResponse_Fileio response = JsonUtility . FromJson < FileUploadResponse_Fileio > ( www . downloadHandler . text ) ;
161- lastReportUploadURI = response . link ;
162- ScreenMessages . PostScreenMessage (
163- $ "Upload complete for <color=#FF8000>{ Path . GetFileName ( lastReportPath ) } </color> to the file.io sharing service (file deleted after first download, 14 days retention)"
164- , 10f , ScreenMessageStyle . UPPER_CENTER , true ) ;
165- CopyLastLinkToClipboard ( ) ;
166- yield break ;
167- }
168- }
169- catch ( Exception )
170- {
171- ScreenMessages . PostScreenMessage ( $ "Upload failed to file.io", 3f , ScreenMessageStyle . UPPER_CENTER , Color . red ) ;
172- }
130+ if ( secondaryService . Success )
131+ {
132+ yield break ;
173133 }
134+
135+ UploadService tertiaryService = new UploadServiceFileio ( fileData , fileName ) ;
136+ yield return HighLogic . fetch . StartCoroutine ( tertiaryService . Send ( ) ) ;
174137 }
175138
176- private static void CopyLastLinkToClipboard ( )
139+ public static void CopyLastLinkToClipboard ( )
177140 {
178141 if ( ! string . IsNullOrEmpty ( lastReportUploadURI ) )
179142 {
@@ -187,18 +150,31 @@ private static void CopyLastLinkToClipboard()
187150 }
188151 }
189152
153+ private static void AddScreenshotToLastReport ( )
154+ {
155+ if ( ! string . IsNullOrEmpty ( lastReportPath ) && File . Exists ( lastReportPath ) )
156+ {
157+ HighLogic . fetch . StartCoroutine ( TakeScreenshot ( ) ) ;
158+ }
159+ else
160+ {
161+ ScreenMessages . PostScreenMessage ( $ "No bug report to add a screenshot to, please create one first.", 3f , ScreenMessageStyle . UPPER_CENTER , Color . red ) ;
162+ }
163+ }
164+
190165 #endregion
191166
192167 #region BUG REPORT CREATION
193168
194169 private static bool CreateReport ( out string zipFileName , out string zipFileFolderPath , out string zipFilePath )
195170 {
196- string logFilePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "KSP.log" ) ;
197- string mmCacheFilePath = Path . Combine ( AppDomain . CurrentDomain . BaseDirectory , "GameData" , "ModuleManager.ConfigCache" ) ;
171+ string rootPath = AppDomain . CurrentDomain . BaseDirectory ;
172+ string logFilePath = Path . Combine ( rootPath , "KSP.log" ) ;
173+ string mmCacheFilePath = Path . Combine ( rootPath , "GameData" , "ModuleManager.ConfigCache" ) ;
198174 zipFileFolderPath = Environment . GetFolderPath ( Environment . SpecialFolder . DesktopDirectory ) ;
199175 if ( string . IsNullOrEmpty ( zipFileFolderPath ) )
200176 {
201- zipFileFolderPath = AppDomain . CurrentDomain . BaseDirectory ;
177+ zipFileFolderPath = rootPath ;
202178 }
203179
204180 zipFileFolderPath = Path . Combine ( zipFileFolderPath , "KSPBugReports" ) ;
@@ -244,6 +220,38 @@ private static bool CreateReport(out string zipFileName, out string zipFileFolde
244220 return false ;
245221 }
246222
223+ string kspLogsPath = Path . Combine ( rootPath , "Logs" ) ;
224+ string kopernicusLogsPath = Path . Combine ( kspLogsPath , "Kopernicus" ) ;
225+ if ( Directory . Exists ( kopernicusLogsPath ) )
226+ {
227+ try
228+ {
229+ string [ ] kopernicusLogs = Directory . GetFiles ( kopernicusLogsPath , "*.log" , SearchOption . AllDirectories ) ;
230+
231+ DateTime startTime = System . Diagnostics . Process . GetCurrentProcess ( ) . StartTime ;
232+ foreach ( string kopernicusLog in kopernicusLogs )
233+ {
234+ using ( Stream fileStream = File . Open ( kopernicusLog , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
235+ {
236+ string entryPath = kopernicusLog . Replace ( kspLogsPath , string . Empty ) . TrimStart ( Path . DirectorySeparatorChar , Path . AltDirectorySeparatorChar ) ;
237+ if ( File . GetLastWriteTime ( kopernicusLog ) < startTime )
238+ {
239+ entryPath += ".oldsession" ;
240+ }
241+ ZipArchiveEntry zipArchiveEntry = archive . CreateEntry ( entryPath , System . IO . Compression . CompressionLevel . Optimal ) ;
242+ using ( Stream entryStream = zipArchiveEntry . Open ( ) )
243+ {
244+ fileStream . CopyTo ( entryStream ) ;
245+ }
246+ }
247+ }
248+ }
249+ catch ( Exception e )
250+ {
251+ Lib . Log ( $ "Error writing Kopernicus logs\n { e } ", Lib . LogLevel . Warning ) ;
252+ }
253+ }
254+
247255 try
248256 {
249257 if ( File . Exists ( mmCacheFilePath ) )
@@ -312,6 +320,53 @@ private static bool CreateReport(out string zipFileName, out string zipFileFolde
312320 return true ;
313321 }
314322
323+ private static IEnumerator TakeScreenshot ( )
324+ {
325+ DebugScreen instance = FindObjectOfType < DebugScreen > ( ) ;
326+ if ( instance != null )
327+ {
328+ yield return null ; // prevent error in DebugScreen
329+ instance . Hide ( ) ;
330+ yield return null ; // make sure the debug screen is hidden when we take the screenshot
331+ }
332+
333+ try
334+ {
335+ byte [ ] shotPNG = ScreenCapture . CaptureScreenshotAsTexture ( 1 ) . EncodeToPNG ( ) ;
336+ string pngFileName = null ;
337+
338+ using ( ZipArchive archive = ZipFile . Open ( lastReportPath , ZipArchiveMode . Update ) )
339+ {
340+ using ( Stream memoryStream = new MemoryStream ( shotPNG ) )
341+ {
342+ pngFileName = "Screenshot_" + DateTime . Now . ToString ( @"yyyy-MM-dd_HHmmss" ) + ".png" ;
343+ ZipArchiveEntry zipArchiveEntry =
344+ archive . CreateEntry ( pngFileName , System . IO . Compression . CompressionLevel . Optimal ) ;
345+ using ( Stream entryStream = zipArchiveEntry . Open ( ) )
346+ {
347+ memoryStream . CopyTo ( entryStream ) ;
348+ }
349+ }
350+ }
351+
352+ ScreenMessages . PostScreenMessage (
353+ $ "Screenshot :\n <color=#FF8000>{ pngFileName } </color>\n added to report :\n <color=#FF8000>{ Path . GetFileName ( lastReportPath ) } </color>",
354+ 5f , ScreenMessageStyle . UPPER_CENTER , true ) ;
355+ Lib . Log ( $ "Screenshot { pngFileName } added to report { Path . GetFileName ( lastReportPath ) } ") ;
356+ }
357+ catch ( Exception e )
358+ {
359+ ScreenMessages . PostScreenMessage ( $ "Error creating a screenshot", 5f , ScreenMessageStyle . UPPER_CENTER , Color . red ) ;
360+ Lib . Log ( $ "Error creating screenshot\n { e } ", Lib . LogLevel . Warning ) ;
361+ }
362+
363+ if ( instance != null )
364+ {
365+ yield return null ;
366+ instance . Show ( ) ;
367+ }
368+ }
369+
315370 public static void ForceFlushKspLogToDisk ( )
316371 {
317372 if ( GameSettings . LOG_INSTANT_FLUSH )
0 commit comments