2017-12-18 17:21:39 -08:00
/ *
Copyright ( c ) 2017 , Lars Brubaker , John Lewin
All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
1. Redistributions of source code must retain the above copyright notice , this
list of conditions and the following disclaimer .
2. Redistributions in binary form must reproduce the above copyright notice ,
this list of conditions and the following disclaimer in the documentation
and / or other materials provided with the distribution .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies ,
either expressed or implied , of the FreeBSD Project .
* /
using System ;
using System.IO ;
2018-09-05 13:51:03 -07:00
using System.Linq ;
2018-12-15 08:53:46 -08:00
using System.Reflection ;
2022-07-15 17:28:39 -07:00
using System.Runtime.Loader ;
2017-12-18 17:21:39 -08:00
using MatterHackers.Agg ;
2018-09-05 13:51:03 -07:00
using MatterHackers.Agg.Image ;
using MatterHackers.Agg.Platform ;
2017-12-18 17:21:39 -08:00
using MatterHackers.Agg.UI ;
2018-12-15 08:53:46 -08:00
using MatterHackers.MatterControl.PrinterControls.PrinterConnections ;
2017-12-18 17:21:39 -08:00
namespace MatterHackers.MatterControl
{
public class WindowsPlatformsFeatures : INativePlatformFeatures
{
public bool CameraInUseByExternalProcess { get ; set ; } = false ;
public event EventHandler PictureTaken ;
public void TakePhoto ( string imageFileName )
{
2020-12-20 16:33:58 -08:00
var noCameraImage = new ImageBuffer ( 640 , 480 ) ;
2017-12-18 17:21:39 -08:00
Graphics2D graphics = noCameraImage . NewGraphics2D ( ) ;
graphics . Clear ( Color . White ) ;
graphics . DrawString ( "No Camera Detected" , 320 , 240 , pointSize : 24 , justification : Agg . Font . Justification . Center ) ;
graphics . DrawString ( DateTime . Now . ToString ( ) , 320 , 200 , pointSize : 12 , justification : Agg . Font . Justification . Center ) ;
2020-11-24 18:08:49 -08:00
ImageIO . SaveImageData ( imageFileName , noCameraImage ) ;
2017-12-18 17:21:39 -08:00
PictureTaken ? . Invoke ( null , null ) ;
}
public void OpenCameraPreview ( )
{
2020-12-20 16:33:58 -08:00
// Camera launcher placeholder (KP)
2017-12-18 17:21:39 -08:00
if ( ApplicationSettings . Instance . get ( ApplicationSettingsKey . HardwareHasCamera ) = = "true" )
{
2020-12-20 16:33:58 -08:00
// Do something
2017-12-18 17:21:39 -08:00
}
else
{
2020-12-20 16:33:58 -08:00
// Do something else (like show warning message)
2017-12-18 17:21:39 -08:00
}
}
public void PlaySound ( string fileName )
{
if ( AggContext . OperatingSystem = = OSType . Windows )
{
2020-11-25 07:39:36 -08:00
using ( var mediaStream = StaticData . Instance . OpenStream ( Path . Combine ( "Sounds" , fileName ) ) )
2017-12-18 17:21:39 -08:00
{
2020-12-20 16:33:58 -08:00
new System . Media . SoundPlayer ( mediaStream ) . Play ( ) ;
2017-12-18 17:21:39 -08:00
}
}
}
public bool IsNetworkConnected ( )
{
return true ;
}
public void ConfigureWifi ( )
{
}
public void ProcessCommandline ( )
{
var commandLineArgs = Environment . GetCommandLineArgs ( ) ;
2018-03-10 21:54:04 -08:00
2018-08-02 17:57:31 -07:00
#if DEBUG
WinformsEventSink . AllowInspector = true ;
#endif
2017-12-18 17:21:39 -08:00
for ( int currentCommandIndex = 0 ; currentCommandIndex < commandLineArgs . Length ; currentCommandIndex + + )
{
string command = commandLineArgs [ currentCommandIndex ] ;
2018-08-02 17:57:31 -07:00
switch ( command . ToUpper ( ) )
2017-12-18 17:21:39 -08:00
{
case "FORCE_SOFTWARE_RENDERING" :
2018-08-23 11:34:06 -07:00
RootSystemWindow . UseGl = false ;
2017-12-18 17:21:39 -08:00
break ;
case "CLEAR_CACHE" :
2018-06-10 15:36:50 -07:00
CacheDirectory . DeleteCacheData ( ) ;
2017-12-18 17:21:39 -08:00
break ;
case "SHOW_MEMORY" :
2017-12-18 17:27:28 -08:00
RootSystemWindow . ShowMemoryUsed = true ;
2017-12-18 17:21:39 -08:00
break ;
2018-08-02 17:57:31 -07:00
case "ALLOW_INSPECTOR" :
WinformsEventSink . AllowInspector = true ;
break ;
2017-12-18 17:21:39 -08:00
}
}
}
public void PlatformInit ( Action < string > reporter )
{
2020-11-25 07:39:36 -08:00
if ( AggContext . OperatingSystem = = OSType . Mac )
2017-12-18 17:21:39 -08:00
{
// Set working directory - this duplicates functionality in Main but is necessary on OSX as Main fires much later (after the constructor in this case)
// resulting in invalid paths due to path tests running before the working directory has been overridden. Setting the value before initializing StaticData
// works around this architectural difference.
Directory . SetCurrentDirectory ( Path . GetDirectoryName ( System . Reflection . Assembly . GetEntryAssembly ( ) . Location ) ) ;
}
2018-09-05 13:21:25 -07:00
if ( Clipboard . Instance = = null )
{
Clipboard . SetSystemClipboard ( new WindowsFormsClipboard ( ) ) ;
}
WinformsSystemWindow . InspectorCreator = ( inspectingWindow ) = >
{
if ( inspectingWindow = = AppContext . RootSystemWindow )
{
// If this is MatterControlApplication, include Scene
var partContext = ApplicationController . Instance . DragDropData ;
return new InspectForm ( inspectingWindow , partContext . SceneContext ? . Scene ? ? null , partContext . View3DWidget ) ;
}
else
{
// Otherwise, exclude Scene
return new InspectForm ( inspectingWindow ) ;
}
} ;
2017-12-18 17:21:39 -08:00
ApplicationSettings . Instance . set ( "HardwareHasCamera" , "false" ) ;
}
2018-09-05 13:21:25 -07:00
public void GenerateLocalizationValidationFile ( )
{
2020-11-25 07:39:36 -08:00
if ( StaticData . Instance is StaticData fileSystemStaticData )
2018-09-05 13:21:25 -07:00
{
char currentChar = 'A' ;
// Note: Functionality only expected to work on Desktop/Debug builds and as such, is coupled to FileSystemStaticData
string outputPath = fileSystemStaticData . MapPath ( Path . Combine ( "Translations" , "L10N" , "Translation.txt" ) ) ;
string sourceFilePath = fileSystemStaticData . MapPath ( Path . Combine ( "Translations" , "Master.txt" ) ) ;
// Ensure the output directory exists
Directory . CreateDirectory ( Path . GetDirectoryName ( outputPath ) ) ;
using ( var outstream = new StreamWriter ( outputPath ) )
{
foreach ( var line in File . ReadAllLines ( sourceFilePath ) )
{
if ( line . StartsWith ( "Translated:" ) )
{
var pos = line . IndexOf ( ':' ) ;
var segments = new string [ ]
{
line . Substring ( 0 , pos ) ,
line . Substring ( pos + 1 ) ,
} ;
outstream . WriteLine ( "{0}:{1}" , segments [ 0 ] , new string ( segments [ 1 ] . ToCharArray ( ) . Select ( c = > c = = ' ' ? ' ' : currentChar ) . ToArray ( ) ) ) ;
if ( currentChar + + = = 'Z' )
{
currentChar = 'A' ;
}
}
else
{
outstream . WriteLine ( line ) ;
}
}
}
}
}
2018-12-15 08:53:46 -08:00
public void InitPluginFinder ( )
{
string searchPath = Path . GetDirectoryName ( System . Reflection . Assembly . GetExecutingAssembly ( ) . Location ) ;
// Load plugins from all dlls in the startup directory
foreach ( var file in Directory . GetFiles ( searchPath , "*.dll" ) )
{
try
{
2022-07-15 17:28:39 -07:00
// Be sure not to load a DLL more than once!
// https://github.com/dotnet/runtime/issues/39783
PluginFinder . LoadTypesFromAssembly ( AssemblyLoadContext . Default . LoadFromAssemblyPath ( file ) ) ;
2018-12-15 08:53:46 -08:00
}
catch ( Exception ex )
{
System . Diagnostics . Debug . WriteLine ( "Error loading assembly: " + ex . Message ) ;
}
}
}
public GuiWidget GetConnectDevicePage ( object printer )
{
return new SetupStepComPortOne ( printer as PrinterConfig ) ;
}
// Primarily required for Android, return true on non-Android platforms
public bool HasPermissionToDevice ( object printer ) = > true ;
2017-12-18 17:21:39 -08:00
}
}