Advertisement
Guest User

Untitled

a guest
Dec 25th, 2018
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.08 KB | None | 0 0
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4.  
  5. namespace BitStrap
  6. {
  7.     public sealed class SteamUploadBuildAction : UMakeBuildAction
  8.     {
  9.         private const string steamCmdArgFormat = "+login {0} {1} +run_app_build_http \"{2}\" +quit";
  10.         public string contentSubFolder = "windows_content/x86";
  11.         public string buildScript = "app_build_<steam_appid>.vdf";
  12.         public bool skipSteamContentCopy = false;
  13.  
  14.         private EditorPrefString steamSdkPath = new EditorPrefString( "UMakeSteam_SteamSdkPath" );
  15.         private EditorPrefString steamUsername = new EditorPrefString( "UMakeSteam_Username" );
  16.         private EditorPrefString steamPassword = new EditorPrefString( "UMakeSteam_Password" );
  17.  
  18.         public override void Execute( UMake umake, UMakeTarget target )
  19.         {
  20.             try
  21.             {
  22.                 string buildPath = UMake.GetBuildPath();
  23.                 string steamBuildScript = buildScript;
  24.                 string sdkPath = steamSdkPath.Value;
  25.                 string username = steamUsername.Value;
  26.                 string password = steamPassword.Value;
  27.                 bool skipCopy = skipSteamContentCopy;
  28.  
  29.  
  30.                 // Check flags for steamBuildScript
  31.                 var projectPath = Application.dataPath.Replace("/Assets", "");
  32.                 steamBuildScript = steamBuildScript.Replace("[ROOT]", projectPath);
  33.                
  34.                 if( UMakeCli.IsInCli )
  35.                 {
  36.                     UMakeCli.Args.TryGetValue( "path", out buildPath );
  37.                     UMakeCli.Args.TryGetValue( "script", out steamBuildScript );
  38.                     UMakeCli.Args.TryGetValue( "steam-sdk", out sdkPath );
  39.                     UMakeCli.Args.TryGetValue( "steam-username", out username );
  40.                     UMakeCli.Args.TryGetValue( "steam-password", out password );
  41.  
  42.                     string skipCopyStringValue;
  43.                     UMakeCli.Args.TryGetValue( "skip-steam-content-copy", out skipCopyStringValue );
  44.                     bool.TryParse( skipCopyStringValue, out skipCopy );
  45.                 }
  46.  
  47.                 if( !Directory.Exists( sdkPath ) )
  48.                 {
  49.                     Debug.LogFormat( "SteamSDK \"{0}\" not found.", sdkPath );
  50.                     return;
  51.                 }
  52.  
  53.                 string steamCmdPath = Path.Combine( sdkPath, "tools/ContentBuilder/builder/steamcmd.exe" ); ;
  54.                 if( !File.Exists( steamCmdPath ) )
  55.                 {
  56.                     Debug.LogFormat( "SteamCMD \"{0}\" not found.", steamCmdPath );
  57.                     return;
  58.                 }
  59.  
  60.                 if( !skipCopy && !CopyContent( sdkPath, target, umake.version, buildPath ) )
  61.                 {
  62.                     Debug.Log( "Could not copy content to Steam folder." );
  63.                     return;
  64.                 }
  65.  
  66.                 var uploaderProcess = new System.Diagnostics.Process();
  67.                 uploaderProcess.StartInfo.FileName = steamCmdPath;
  68.                 uploaderProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName( Path.GetDirectoryName( steamCmdPath ) );
  69.                 uploaderProcess.StartInfo.Arguments = string.Format( steamCmdArgFormat, username, password, steamBuildScript );
  70.  
  71.                 if( UMakeCli.IsInCli )
  72.                 {
  73.                     uploaderProcess.StartInfo.UseShellExecute = false;
  74.                     uploaderProcess.StartInfo.RedirectStandardOutput = true;
  75.                     uploaderProcess.OutputDataReceived += ( sender, msg ) =>
  76.                     {
  77.                         if( msg != null )
  78.                             Debug.Log( msg.Data );
  79.                     };
  80.                 }
  81.  
  82.                 uploaderProcess.Start();
  83.                 Debug.LogFormat( "Executing SteamCMD \"{0}\"...", steamCmdPath );
  84.  
  85.                 if( UMakeCli.IsInCli )
  86.                 {
  87.                     uploaderProcess.BeginOutputReadLine();
  88.                 }
  89.  
  90.                 uploaderProcess.WaitForExit();
  91.                 uploaderProcess.Close();
  92.             }
  93.             catch( System.Exception e )
  94.             {
  95.                 Debug.Log( "Upload to Steam failed." );
  96.                 Debug.LogException( e );
  97.             }
  98.         }
  99.  
  100.         public override void OnInspectorGUI( UMakeBuildActionEditor editor )
  101.         {
  102.             base.OnInspectorGUI( editor );
  103.  
  104.             using( BoxGroup.Do( "Shared Settings" ) )
  105.             {
  106.                 using( Horizontal.Do() )
  107.                 {
  108.                     steamSdkPath.Value = EditorGUILayout.TextField( "Steam SDK Folder", steamSdkPath.Value );
  109.                     if( GUILayout.Button( "Change", GUILayout.Width( 64.0f ) ) )
  110.                     {
  111.                         string path = EditorUtility.OpenFolderPanel( "Steam SDK Folder Path", steamSdkPath.Value, "" );
  112.                         if( !string.IsNullOrEmpty( path ) )
  113.                             steamSdkPath.Value = path;
  114.                     }
  115.                 }
  116.  
  117.                 steamUsername.Value = EditorGUILayout.TextField( "Steam Username", steamUsername.Value );
  118.                 steamPassword.Value = EditorGUILayout.PasswordField( "Steam Password", steamPassword.Value );
  119.             }
  120.  
  121.             GUILayout.FlexibleSpace();
  122.         }
  123.  
  124.         private bool CopyContent( string sdkPath, UMakeTarget umakeTarget, string version, string buildPath )
  125.         {
  126.             string contentFolderPath = Path.Combine( sdkPath, "tools/ContentBuilder/content" );
  127.  
  128.             if( !Directory.Exists( contentFolderPath ) )
  129.             {
  130.                 Debug.LogFormat( "Content folder \"{0}\" does not exist.", contentFolderPath );
  131.                 return false;
  132.             }
  133.  
  134.             UMakeTarget.Path targetPath = umakeTarget.GetTargetPath( version, buildPath );
  135.  
  136.             if( !Directory.Exists( targetPath.directoryPath ) )
  137.             {
  138.                 Debug.LogFormat( "Target path \"{0}\" does not exist. Did you forget to build?", targetPath.directoryPath );
  139.                 return false;
  140.             }
  141.  
  142.             string uploadFolderPath = Path.Combine( contentFolderPath, contentSubFolder );
  143.             if( Directory.Exists( uploadFolderPath ) )
  144.                 Directory.Delete( uploadFolderPath, true );
  145.  
  146.             FileSystemHelper.CopyDirectory( targetPath.directoryPath, uploadFolderPath );
  147.  
  148.             Debug.LogFormat( "Build files copied to \"{0}\".", uploadFolderPath );
  149.             return true;
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement