Files
UnrealEngineUWP/Engine/Source/Programs/AutomationTool/HTML5/HTML5Platform.Automation.cs
chris babcock 016d94e747 Copying //UE4/Dev-Mobile to //UE4/Dev-Main (Source: //UE4/Dev-Mobile [at] 4355761)
#lockdown Nick.Penwarden
#rb Jack.Porter

============================
  MAJOR FEATURES & CHANGES
============================

Change 4066430 by Nick.Shin

	HTML5 - fix merge stomping and add in missing SC.Metadatadir (null) object to last {if else} block

	#jira UE-58423  HTML5 Quicklaunch through UnrealFrontEnd fails on Stage Command: Error: System.NullReferenceExeception

Change 4067037 by Bogdan.Vasilache

	#jira UE-57406

	Shaders compiled for GLSL_ES2 with CFLAG_FeatureLevelES31 compiler flag will now shift to GLSL_ES3_1_ANDROID profile instead of GLSL_310_ES_EXT.

Change 4067649 by Jack.Porter

	Remove obsolete cvars from ini files
	#jira UE-54768

Change 4071833 by Bogdan.Vasilache

	#jira UE-57406

	ShaderVersion guid updated to reflect changes in the OpenGL shader compiler.

Change 4075162 by Bogdan.Vasilache

	#jira UE-53195

Change 4099176 by Jack.Porter

	Fix SpeedTree node not working on ES3_1 (Contributed by PicaroonX)

	#jira UE-59685
	PR #4761

Change 4124398 by Mi.Wang

	Add particle cutout support for feature level es31.

Change 4129400 by Sorin.Gradinaru

	UE-60226 Web Browser widget is completely white on Android

	#jira UE-60226
	#jira UE-53247
	#Android
	#4.21

	from Release-4.20 CL 4122319

	The URL from the sample app is malformed (http:www.google.com).
	Since the phone's default browser is able to correct this problem for http and https URLs, I've added this exception to the code that checks the URL prefix, re-writing the fix for UE-53247.

Change 4164212 by Nick.Shin

	minor fixes to merge from Main

	#jira none

Change 4165606 by Nick.Shin

	#jira UE-57006  QA-Promotion WebGL1 HTML5 Launch On has numerous graphical errors

	in order for UNROLL to work -- break statement needs to be removed

Change 4165823 by Nick.Shin

	HTML5 - split single large template file to smaller component (e.g. js & css) files

	custom template files are also supported on a per-project basis:

	copy
	.../Engine/Build/HTML5/GameX.*.template
	to
	<project>/Build/HTML5/.

	build will automatically pick it the project's path (otherwise fallback to the Engine's version)

	this is based on GitHub PR #4780

	#jira UE-60136  GitHub 4780 : Proposed HTML5 Changes

Change 4202481 by Sorin.Gradinaru

	UE-50492 OnUrlChanged does not fire on mobile

	#jira UE-50492
	#iOS
	#4.21

	Adding the events for load started & finished with errors

Change 4202484 by Sorin.Gradinaru

	UEMOB-454 Move the virtual keyboard out of Experimental and make it enabled by default

	#jira UEMOB-454
	#4.21

Change 4202583 by Bogdan.Vasilache

	[UE-53816] Allow texture sampling in vertex shaders for ES2 feature level
	#jira UE-53816

Change 4214064 by Nick.Shin

	Epic's GitHub policy is to treat "unknown" file extensions as "binary"

	changing filenames so that they will show up GitHub's repo

	this is in reference to GitHub PR #4780

	#jira UE-60136  GitHub 4780 : Proposed HTML5 Changes

Change 4224514 by Mi.Wang

	We should not Tick USceneCaptureComponent on DS by default.

Change 4230192 by Nick.Shin

	HTML5 READMEs

	updating old ones - and putting new ones in

	these originally came from (mostly) onboarding the chrome team on UE4 emails.  capturing most of that in markdown format.

	#jria UEMOB-255  HTML5 Multithreading

Change 4232460 by Nick.Shin

	HTML5 - fix UE_MakeHTTPDataRequest() crash

	#jira  UE-38351  ( Pri:1 - 4.21 )  Red and Blue color channels flipped on materials called from HTML5 server

Change 4234647 by Nick.Shin

	HTML5 - ERGBFormat::RGBA  vs  ERGBFormat::BGRA

	- make use of the enums in UAsyncTaskDownloadImage::HandleImageRequest()
	- enable RGBA  vs  BGRA options for JPG decoder

	#jira  UE-38351  ( Pri:1 - 4.21 )  Red and Blue color channels flipped on materials called from HTML5 server

Change 4240086 by Sorin.Gradinaru

	UE-61345 Decals in Remote Session are not rendering on some iOS devices

	#jira UE-61345
	#4.21
	#iOS

	Changed the texture compression from DXT5 to RGBA

Change 4240234 by Sorin.Gradinaru

	UE-61838 Android 'Launch On All' can't run during an existing launch on session on Windows

	#jira UE-61838
	#4.21

	Cancel previous sessions before launching the selected configuration.

Change 4244686 by Nick.Shin

	HTML5 - syncing build scripts

	#jria UEMOB-255  HTML5 Multithreading

Change 4246399 by Nick.Shin

	HTML5 - syncing PhysX build scripts

	#jria UEMOB-255  HTML5 Multithreading

Change 4267990 by Bogdan.Vasilache

	[UEMOB-405] - Mobile PIE polishing - Bezel & Rotation + related subtasks

	#jira UEMOB-405
	#jira UEMOB-458
	#jira UEMOB-459
	#jira UEMOB-460

Change 4280211 by Chris.Babcock

	Fix FD_SET macro invoke crash when Socket FD exceed 1023 (contributed by scahp)

	#jira UE-62290
	#PR #4912
	#ue4
	#android

Change 4300275 by Nick.Shin

	HTML5 - Unreal File Server - css response header fixes

	#jira UE-63009 HTML5 template page has non-centered viewport and loading banner stretches the entire screen

Change 4302328 by Nick.Shin

	#jira UE-63108  HTML5 RunMacHTML5LauncherHelper.command files do not have appropriate permissions to run on Mac

Change 4303428 by Jack.Porter

	Fixed crash during cooking for certain landscapes with collision data out of sync with render data.

Change 4307517 by Jack.Porter

	Fix warning on Mac/Linux

	#jira UE-62993

Change 4311437 by Nick.Shin

	HTML5 : re-enabling [ -s BINARYEN_TRAP_MODE='clamp'; ]

	warning: this is technically slower performing -- but, this fixes a lot of 64-bit issues (HTML5 is "only" 32-bits)

	#jira UE-63049  HTML5 Quicklaunch to Chrome crashes with RuntimeError: float unrepresentable in integer range

Change 4312602 by Chris.Babcock

	Allow some hardware buttons through if hardware keyboard disabled

	#jira UE-63260
	#ue4
	#android

Change 4312711 by Chris.Babcock

	Fix string formatting for some locales in GetMetaDataString Method (contributed by IlinAleksey)

	#jira UE-63252
	#PR #5026
	#ue4
	#android

Change 4321719 by Bogdan.Vasilache

	UE-63378 - Fixed static analysis warnings
	#jira UE-63378

Change 4331224 by Jack.Porter

	Fixed Mobile PIE resolution for iPhone 8 Plus

	#jira UE-63191

Change 4339446 by Jack.Porter

	Modified foliage thumbnail color to better show when it is inactive
	#jira UE-30824
	#5057

Change 4339458 by Dmitriy.Dyomin

	Fixed typos in glObjectPtrLabel type definitions (contributed by TheCodez)
	#4920
	#jira UE-62368

Change 4339460 by Dmitriy.Dyomin

	Fixed: bRenderInMainPass on SkeletalMeshes doesn't work on Mobile
	#jira UE-59077

Change 4339523 by Sorin.Gradinaru

	UE-61351 Error: Failed to send -1 bytes of data to RemoteConnection spammed in the editor log when closing RemoteSession on device during connection

	#jira UE-61351
	#4.21

	Not an real issue. The message is issued whenever the host (or the client) tries to send messages through an inactive channel. There is a "/ping" message sent back and forth between the host and the client, with high frequency (hence the spam) - if this message is no longer received, the host (or the client) decides to terminate the connection.

	"Downgrading" the message from Error to Log, to avoid spamming the log on release builds.

Change 4339590 by Sorin.Gradinaru

	UE-61554 Web Browser: No Transparency in 3D Widget Component on Mobile

	#jira UE-61554
	#Android
	#4.21

	Modified Web Texture Material's blend mode to "Translucent"

Change 4339595 by Sorin.Gradinaru

	[Build] UE4 Dev-Mobile - CL 4339590 - Incremental Editor Win64

Change 4339597 by Jack.Porter

	Fix static analysis CIS
	This function should be moved to SceneUtils.h and the dependency in AsyncTaskDownloadImage resolved another way.
	#jira UE-63378

Change 4339599 by Sorin.Gradinaru

	UE-56076 Android Launch On "Running..." toast intermittently doesn't appear
	#jira UE-56076
	#Android
	#4.21

	wait before getting the process list with "adb shell ps" - on some devices the list is not yet ready

Change 4339647 by Sorin.Gradinaru

	UE-54791 Planar Reflection gamma / brightness incorrect on Samsung Galaxy S5

	#jira UE-54791
	#Android
	#4.21

	The planar texture contains encoded data:
	- decode it immediately after sampling the texture
	- alpha channel was inverted during the PrefilterPlanarReflectionPS step

Change 4339654 by Sorin.Gradinaru

	UE-59697 WebBrowser SetVisibility not working on Android & iOS

	#jira UE-59697
	#4.21
	#iOS
	#Android

	SetVisibility implemented on iOS and Android's web browsers, by extending the implementation on Windows: use a list to store references to the webbrowsers in the scene , then in WebBrowserSingleton::Tick check if the cached web browsers windows or widgets ticked on the last frame

Change 4339656 by Sorin.Gradinaru

	Remove unnecessary comment in IOSPlatformWebBrowser.cpp

Change 4339666 by Bogdan.Vasilache

	[UEMOB-455] Make editor tool to dump out a device .json file for a connected Android device
	#jira UEMOB-455

Change 4339667 by Bogdan.Vasilache

	Mobile PIE bugfixes
	#jira UE-63288
	#jira UE-63282

Change 4339700 by Bogdan.Vasilache

	[bugfix] Accidentally disabled SetPreviewPlatform() call for mobile PIE simulation.

Change 4339733 by Sorin.Gradinaru

	UE-59381 Ios application default orientation

	#jira UE-59381
	#iOS
	#4.21

	- added Initial interface orientation in the generated Plist file (UIInterfaceOrientation key)
	- implemented preferredInterfaceOrientationForPresentation in the IOSViewController
	- re-arranged the orientation list in the Plist file

Change 4339781 by Sorin.Gradinaru

	UE-60716 Show cursor in Editor text boxes when focused on Android

	#jira UE-60716
	#Android
	#4.21

	Updating the widget's cursor position from the native code.
	This also overrides the fix for UE49124 Cursor in virtual keyboard and UMG don't match - initially the solution was to hide the widget's cursor.

Change 4340037 by Jack.Porter

	Added an Android Project Settings checkbox to enable detection of Vulkan device support by default. If unchecked, the -detectvulkan command line parameter can be used.

Change 4340076 by Jack.Porter

	Fix warning seen by CIS

	#jira UE-63377

Change 4340212 by Sorin.Gradinaru

	UE-63224 Android DLC profile in Project Launcher fails if there is a space in the path for the destination directory

	#jira UE-63224
	#4.21
	#Android

	Avoid enclosing path arguments in double quotes (""<path>""). MakePathSafeToUseWithCommandLine(<path>) already adds quotes when needed.

Change 4340721 by Andrew.Grant

	Fixed -vsmac argument not working with GenerateProjectFiles

	#jira nojira

Change 4340822 by Andrew.Grant

	Replaced default iOS splashcreens with UE logo
	Added missing resolutions (iPhone 5 portrait, iPad 10.5")
	Removed custom splash images from RemoteSessionApp

	#jira UE-59864, UE-61939

Change 4340857 by Chris.Babcock

	Fix virtual keyboard crashes on Android

	#jira none

Change 4341047 by Andrew.Grant

	Prevent Xcode upgrade warnings for >9.0 versions

Change 4343797 by Cosmin.Sulea

	iOS - iPhonePackager ExportCertificate command failing 100%

Change 4343823 by Sorin.Gradinaru

	UE-63334 Unreal Remote 2 does not send information about motion control

	#jira UE-63334
	#iOS
	#Android

	The functionality was commented out. Tested it for performance and it seems OK (despite the high frequency of /motion signals)

Change 4343824 by Sorin.Gradinaru

	UEMOB-185 Implement the equivalent of FAndroidJSScripting for iOS
	UE-59488 FWebBrowserWindow::UnbindUObject of IOSPlatformWebBrowser.cpp is not implemented yet

	#jira UEMOB-185
	#jira UE-59488
	#4.21
	#iOS

	New methods (using WebKit, matching the Android implementation):
	- Go back/forward
	- HandlePageLoading (loading/didCommitNavigation, loaded/didFinishNavigation)
	- HandleReceivedError (didFailNavigation)
	- execute JS code
	- controlling the client using JS commands (WKWebViewConfiguration & WKUserContentController):
		- in Android we using shouldInterceptRequest to intercept a custom resource URL (JS: XMLHttpRequest.send, document.location)
		- iOS will register handlers (JS: window.webkit.messageHandlers.<FMobileJSScripting::JSMessageHandler>.postMessage)

Change 4343845 by Sorin.Gradinaru

	Adding the signatures for FJavaWrapper's CallLongMethod, CallFloatMethod and CallDoubleMethod in the AndroidJNI.h

	#4.21
	#Android

Change 4343893 by Cosmin.Sulea

	UEMOB-465 - Add Device Output Log support for iOS
	#jira UEMOB-465

Change 4343895 by Dmitriy.Dyomin

	Fixed: DepthFade does not work in mobile emulation while MobileMSAA enabled
	#jira UE-60404

Change 4343899 by Dmitriy.Dyomin

	GitHub 4851 : Fix layer info property bNoWeightBlend not being saved after tiled landscape import (contributed by cmp-)
	#4851
	#jira UE-61437

Change 4343906 by Bogdan.Vasilache

	Editor's feature level is now saved and restored between sessions
	#jira UE-55506
	#jira UE-59688

Change 4346729 by Jack.Porter

	Fix CIS
	#jira UE-63716

Change 4347811 by Jack.Porter

	Fix dependency chain while including PIEPreviewDeviceSpecification
	#jira UE-63755

Change 4348580 by Jack.Porter

	Fix CIS issues in IOSPlatformWebBrowser
	#jira UE-63809

Change 4350092 by Jack.Porter

	Fix null pointer exception in iPhonePackager when a matching certificate cannot be found

	#jira UE-63821

Change 4350968 by Jack.Porter

	Update binaries for deployment server
	#jira UEMOB-332

Change 4353925 by Jack.Porter

	Merging //UE4/Dev-Main to Dev-Mobile (//UE4/Dev-Mobile)
	Rework PR 4912 from CL 4280211

	#jira UE-62290

Change 4354034 by Jack.Porter

	Rebuilt deployment server after backing out changes

	#jira UE-63826

Change 4354338 by Ben.Marsh

	Attempted fix for incorrect certificate errors when compiling remotely.

	* Overloaded meaning of the -Certificate argument (ie. as the output path rather than as the desired certificate when selecting a provision) was causing certificate name check to fail. Now uses a different parameter to specify the output path.
	* TVOS argument was not being passed when exporting the certificate, also causing the wrong provision/certificate pair to be selected.
	* Add support for the -bundlename argument, though it shouldn't be necessary when invoked from UBT since the UUID discovered in a previous invocation of IPP is explicitly specified.

	Since the provision UUID is already being specified to IPP, it should be able to identify the correct provision on phase -1 using this info.

	#jira UE-63859

Change 4354459 by Chris.Babcock

	Fix issue with Android PCH include files

	#jira UE-63909
	#ue4
	#android

Change 4255761 by Peter.Sauerbrei

	fix for space in manifest name
	#jira UE-63868

#ROBOMERGE-OWNER: jason.bestimt
#ROBOMERGE-SOURCE: CL 4356719 in //UE4/Main/...
#ROBOMERGE-BOT: DEVVR (Main -> Dev-VR)

[CL 4356730 by chris babcock in Dev-VR branch]
2018-09-10 21:28:08 -04:00

839 lines
33 KiB
C#

// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
using System;
using System.IO;
using System.Net.Http;
using System.Text;
using AutomationTool;
using UnrealBuildTool;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Tools.DotNETCommon;
public class HTML5Platform : Platform
{
// ini configurations
static bool Compressed = false;
static bool enableIndexedDB = false; // experimental for now...
public HTML5Platform()
: base(UnrealTargetPlatform.HTML5)
{
}
public override void Package(ProjectParams Params, DeploymentContext SC, int WorkingCL)
{
LogInformation("Package {0}", Params.RawProjectPath);
LogInformation("Setting Emscripten SDK for packaging..");
HTML5SDKInfo.SetupEmscriptenTemp();
HTML5SDKInfo.SetUpEmscriptenConfigFile();
// ----------------------------------------
// ini configurations
ConfigHierarchy ConfigCache = UnrealBuildTool.ConfigCache.ReadHierarchy(ConfigHierarchyType.Engine, DirectoryReference.FromFile(Params.RawProjectPath), UnrealTargetPlatform.HTML5);
// Debug and Development builds are not compressed to:
// - speed up iteration times
// - ensure (IndexedDB) data are not cached/used
// Shipping builds "can be":
// - compressed
// - (IndexedDB) cached
string ProjectConfiguration = Params.ClientConfigsToBuild[0].ToString();
if (ProjectConfiguration == "Shipping")
{
ConfigCache.GetBool("/Script/HTML5PlatformEditor.HTML5TargetSettings", "Compressed", out Compressed);
ConfigCache.GetBool("/Script/HTML5PlatformEditor.HTML5TargetSettings", "EnableIndexedDB", out enableIndexedDB);
}
LogInformation("HTML5Platform.Automation: Compressed = " + Compressed );
LogInformation("HTML5Platform.Automation: EnableIndexedDB = " + enableIndexedDB );
// ----------------------------------------
// package directory
string PackagePath = Path.Combine(Path.GetDirectoryName(Params.RawProjectPath.FullName), "Binaries", "HTML5");
if (!Directory.Exists(PackagePath))
{
Directory.CreateDirectory(PackagePath);
}
string _ProjectNameExtra = ProjectConfiguration != "Development" ? "-HTML5-" + ProjectConfiguration : "";
string _ProjectFullpath = Params.GetProjectExeForPlatform(UnrealTargetPlatform.HTML5).ToString();
string _ProjectFilename = Path.GetFileNameWithoutExtension(_ProjectFullpath) + _ProjectNameExtra;
string SrcUE4GameBasename = Path.Combine(Path.GetDirectoryName(_ProjectFullpath), _ProjectFilename);
string UE4GameBasename = Path.Combine(PackagePath, _ProjectFilename);
string ProjectBasename = Path.Combine(PackagePath, Params.ShortProjectName + _ProjectNameExtra);
// ----------------------------------------
// packaging
if (HTMLPakAutomation.CanCreateMapPaks(Params))
{
HTMLPakAutomation PakAutomation = new HTMLPakAutomation(Params, SC);
// Create Necessary Paks.
PakAutomation.CreateEnginePak();
PakAutomation.CreateGamePak();
PakAutomation.CreateContentDirectoryPak();
// Create Emscripten Package from Necessary Paks. - This will be the VFS.
PakAutomation.CreateEmscriptenDataPackage(PackagePath, ProjectBasename + ".data");
// Create All Map Paks which will be downloaded on the fly.
PakAutomation.CreateMapPak();
// Create Delta Paks if setup.
List<string> Paks = new List<string>();
ConfigCache.GetArray("/Script/HTML5PlatformEditor.HTML5TargetSettings", "LevelTransitions", out Paks);
if (Paks != null)
{
foreach (var Pak in Paks)
{
var Matched = Regex.Matches(Pak, "\"[^\"]+\"", RegexOptions.IgnoreCase);
string MapFrom = Path.GetFileNameWithoutExtension(Matched[0].ToString().Replace("\"", ""));
string MapTo = Path.GetFileNameWithoutExtension(Matched[1].ToString().Replace("\"", ""));
PakAutomation.CreateDeltaMapPaks(MapFrom, MapTo);
}
}
}
else
{
// we need to operate in the root
string PythonPath = HTML5SDKInfo.Python();
string EmPackagerPath = HTML5SDKInfo.EmscriptenPackager();
using (new ScopedEnvVar("EM_CONFIG", HTML5SDKInfo.DOT_EMSCRIPTEN))
{
using (new PushedDirectory(Path.Combine(Params.BaseStageDirectory, "HTML5")))
{
string CmdLine = string.Format("\"{0}\" \"{1}\" --preload . --js-output=\"{1}.js\" --no-heap-copy", EmPackagerPath, ProjectBasename + ".data");
RunAndLog(CmdEnv, PythonPath, CmdLine);
}
}
}
// ----------------------------------------
// copy to package directory
// ensure the ue4game binary exists, if applicable
if (!FileExists_NoExceptions(SrcUE4GameBasename + ".js"))
{
LogInformation("Failed to find game application " + SrcUE4GameBasename + ".js");
throw new AutomationException(ExitCode.Error_MissingExecutable, "Stage Failed. Could not find application {0}. You may need to build the UE4 project with your target configuration and platform.", SrcUE4GameBasename + ".js");
}
if ( !Params.IsCodeBasedProject )
{
// template project - need to copy over UE4Game.*
File.Copy(SrcUE4GameBasename + ".wasm", UE4GameBasename + ".wasm", true);
File.Copy(SrcUE4GameBasename + ".js", UE4GameBasename + ".js", true);
File.Copy(SrcUE4GameBasename + ".js.symbols", UE4GameBasename + ".js.symbols", true);
File.SetAttributes(UE4GameBasename + ".wasm", FileAttributes.Normal);
File.SetAttributes(UE4GameBasename + ".js" , FileAttributes.Normal);
File.SetAttributes(UE4GameBasename + ".js.symbols", FileAttributes.Normal);
}
// else, c++ projects will compile "to" PackagePath
// note: ( ProjectBasename + ".data" ) already created above (!HTMLPakAutomation.CanCreateMapPaks())
// ----------------------------------------
// generate HTML files to the package directory
// custom HTML, JS (if any), and CSS (if any) template files
string LocalBuildPath = CombinePaths(CmdEnv.LocalRoot, "Engine/Build/HTML5");
string BuildPath = Path.Combine(Path.GetDirectoryName(Params.RawProjectPath.FullName), "Build", "HTML5");
string TemplateFile = CombinePaths(BuildPath, "project_template.html");
if ( !File.Exists(TemplateFile) )
{
// fall back to default UE4 template files
BuildPath = LocalBuildPath;
TemplateFile = CombinePaths(BuildPath, "project_template.html");
}
GenerateFileFromTemplate(TemplateFile, ProjectBasename + ".html", Params, ConfigCache);
TemplateFile = CombinePaths(BuildPath, "project_template.js");
if ( File.Exists(TemplateFile) )
{
GenerateFileFromTemplate(TemplateFile, ProjectBasename + ".UE4.js", Params, ConfigCache);
}
TemplateFile = CombinePaths(BuildPath, "project_template.css");
if ( File.Exists(TemplateFile) )
{
GenerateFileFromTemplate(TemplateFile, ProjectBasename + ".css", Params, ConfigCache);
}
// ----------------------------------------
// (development) support files
string MacBashTemplateFile = CombinePaths(LocalBuildPath, "RunMacHTML5LaunchHelper_template.command");
string MacBashOutputFile = Path.Combine(PackagePath, "RunMacHTML5LaunchHelper.command");
string MonoPath = CombinePaths(CmdEnv.LocalRoot, "Engine/Build/BatchFiles/Mac/SetupMono.sh");
GenerateMacCommandFromTemplate(MacBashTemplateFile, MacBashOutputFile, MonoPath);
// ........................................
string htaccesspath = Path.Combine(PackagePath, ".htaccess");
if ( File.Exists(htaccesspath) )
{
FileAttributes attributes = File.GetAttributes(htaccesspath);
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
attributes &= ~FileAttributes.ReadOnly;
File.SetAttributes(htaccesspath, attributes);
}
}
File.Copy(CombinePaths(LocalBuildPath, "htaccess_template.txt"), htaccesspath, true);
// ----------------------------------------
// final copies
// Gather utlity .js files and combine into one file
string DestinationFile = PackagePath + "/Utility.js";
File.Delete(DestinationFile);
// spelling this out - one file at a time (i.e. don't slurp in project_template.js)
File.AppendAllText(DestinationFile, File.ReadAllText(CombinePaths(LocalBuildPath, "json2.js")));
File.AppendAllText(DestinationFile, File.ReadAllText(CombinePaths(LocalBuildPath, "jstorage.js")));
File.AppendAllText(DestinationFile, File.ReadAllText(CombinePaths(LocalBuildPath, "moz_binarystring.js")));
if (Compressed)
{
LogInformation("Build configuration is " + ProjectConfiguration + ", so (gzip) compressing files for web servers.");
// Compress all files. These are independent tasks which can be threaded.
List<Task> CompressionTasks = new List<Task>();
CompressionTasks.Add(Task.Factory.StartNew(() => CompressFile(UE4GameBasename + ".wasm", UE4GameBasename + ".wasmgz"))); // main game code
CompressionTasks.Add(Task.Factory.StartNew(() => CompressFile(UE4GameBasename + ".js", UE4GameBasename + ".jsgz"))); // main js (emscripten)
CompressionTasks.Add(Task.Factory.StartNew(() => CompressFile(UE4GameBasename + ".js.symbols", UE4GameBasename + ".js.symbolsgz"))); // symbols fil.
CompressionTasks.Add(Task.Factory.StartNew(() => CompressFile(PackagePath + "/Utility.js", PackagePath + "/Utility.jsgz"))); // Utility
CompressionTasks.Add(Task.Factory.StartNew(() => CompressFile(ProjectBasename + ".data", ProjectBasename + ".datagz"))); // DATA file
CompressionTasks.Add(Task.Factory.StartNew(() => CompressFile(ProjectBasename + ".data.js" , ProjectBasename + ".data.jsgz"))); // DATA file .js driver (emscripten)
if ( File.Exists(ProjectBasename + ".UE4.js") )
{
CompressionTasks.Add(Task.Factory.StartNew(() => CompressFile(ProjectBasename + ".UE4.js" , ProjectBasename + ".UE4.jsgz"))); // UE4 js
}
if ( File.Exists(ProjectBasename + ".css") )
{
CompressionTasks.Add(Task.Factory.StartNew(() => CompressFile(ProjectBasename + ".css" , ProjectBasename + ".cssgz"))); // UE4 css
}
Task.WaitAll(CompressionTasks.ToArray());
}
else
{
LogInformation("Build configuration is " + ProjectConfiguration + ", so not compressing. Build Shipping configuration to compress files to save space.");
// nuke old compressed files to prevent using stale files
File.Delete(UE4GameBasename + ".wasmgz");
File.Delete(UE4GameBasename + ".jsgz");
File.Delete(UE4GameBasename + ".js.symbolsgz");
File.Delete(PackagePath + "/Utility.jsgz");
File.Delete(ProjectBasename + ".datagz");
File.Delete(ProjectBasename + ".data.jsgz");
File.Delete(ProjectBasename + ".UE4.jsgz");
File.Delete(ProjectBasename + ".cssgz");
}
File.Copy(CombinePaths(CmdEnv.LocalRoot, "Engine/Binaries/DotNET/HTML5LaunchHelper.exe"),CombinePaths(PackagePath, "HTML5LaunchHelper.exe"),true);
// Task.WaitAll(CompressionTasks);
PrintRunTime();
}
void CompressFile(string Source, string Destination)
{
LogInformation(" Compressing " + Source);
bool DeleteSource = false;
if( Source == Destination )
{
string CopyOrig = Source + ".Copy";
File.Copy(Source, CopyOrig);
Source = CopyOrig;
DeleteSource = true;
}
using (System.IO.Stream input = System.IO.File.OpenRead(Source))
{
using (var raw = System.IO.File.Create(Destination))
{
using (Stream compressor = new Ionic.Zlib.GZipStream(raw, Ionic.Zlib.CompressionMode.Compress,Ionic.Zlib.CompressionLevel.BestCompression))
{
byte[] buffer = new byte[2048];
int SizeRead = 0;
while ((SizeRead = input.Read(buffer, 0, buffer.Length)) != 0)
{
compressor.Write(buffer, 0, SizeRead);
}
}
}
}
if (DeleteSource)
{
File.Delete(Source);
}
}
protected void GenerateFileFromTemplate(string InTemplateFile, string InOutputFile, ProjectParams Params, ConfigHierarchy ConfigCache)
{
bool IsContentOnly = !Params.IsCodeBasedProject;
string ProjectConfiguration = Params.ClientConfigsToBuild[0].ToString();
string UE4GameName = IsContentOnly ? "UE4Game" : Params.ShortProjectName;
string ProjectName = Params.ShortProjectName;
if (ProjectConfiguration != "Development")
{
UE4GameName += "-HTML5-" + ProjectConfiguration;
ProjectName += "-HTML5-" + ProjectConfiguration;
}
string CanvasScaleMode;
ConfigCache.GetString("/Script/HTML5PlatformEditor.HTML5TargetSettings", "CanvasScalingMode", out CanvasScaleMode);
string HeapSize = HTML5SDKInfo.HeapSize(ConfigCache, ProjectConfiguration).ToString();
StringBuilder outputContents = new StringBuilder();
using (StreamReader reader = new StreamReader(InTemplateFile))
{
string LineStr = null;
while (reader.Peek() != -1)
{
LineStr = reader.ReadLine();
if (LineStr.Contains("%TIMESTAMP%"))
{
string TimeStamp = DateTime.UtcNow.ToString("yyyyMMddHHmm");
LineStr = LineStr.Replace("%TIMESTAMP%", TimeStamp);
}
if (LineStr.Contains("%SHORTNAME%"))
{
LineStr = LineStr.Replace("%SHORTNAME%", Params.ShortProjectName);
}
if (LineStr.Contains("%UE4GAMENAME%"))
{
LineStr = LineStr.Replace("%UE4GAMENAME%", UE4GameName);
}
if (LineStr.Contains("%PROJECTNAME%"))
{
LineStr = LineStr.Replace("%PROJECTNAME%", ProjectName);
}
if (LineStr.Contains("%SERVE_COMPRESSED%"))
{
LineStr = LineStr.Replace("%SERVE_COMPRESSED%", Compressed ? "true" : "false");
}
if (LineStr.Contains("%DISABLE_INDEXEDDB%"))
{
LineStr = LineStr.Replace("%DISABLE_INDEXEDDB%",
enableIndexedDB ? "" : "enableReadFromIndexedDB = false;\nenableWriteToIndexedDB = false;");
}
if (LineStr.Contains("%HEAPSIZE%"))
{
LineStr = LineStr.Replace("%HEAPSIZE%", HeapSize + " * 1024 * 1024");
}
if (LineStr.Contains("%UE4CMDLINE%"))
{
string ArgumentString = "'../../../" + Params.ShortProjectName + "/" + Params.ShortProjectName + ".uproject',";
ArgumentString += "'-stdout',"; // suppress double printing to console.log
LineStr = LineStr.Replace("%UE4CMDLINE%", ArgumentString);
}
if (LineStr.Contains("%CANVASSCALEMODE%"))
{
string mode = "2 /*ASPECT*/"; // default
if ( CanvasScaleMode.Equals("stretch", StringComparison.InvariantCultureIgnoreCase))
{
mode = "1 /*STRETCH*/";
}
else if ( CanvasScaleMode.Equals("fixed", StringComparison.InvariantCultureIgnoreCase))
{
mode = "3 /*FIXED*/";
}
LineStr = LineStr.Replace("%CANVASSCALEMODE%", mode);
}
outputContents.AppendLine(LineStr);
}
}
if (outputContents.Length > 0)
{
// Save the file
try
{
Directory.CreateDirectory(Path.GetDirectoryName(InOutputFile));
File.WriteAllText(InOutputFile, outputContents.ToString(), Encoding.UTF8);
}
catch (Exception)
{
// Unable to write to the project file.
}
}
}
protected void GenerateMacCommandFromTemplate(string InTemplateFile, string InOutputFile, string InMonoPath)
{
StringBuilder outputContents = new StringBuilder();
using (StreamReader reader = new StreamReader(InTemplateFile))
{
string InMonoPathParent = Path.GetDirectoryName(InMonoPath);
string LineStr = null;
while (reader.Peek() != -1)
{
LineStr = reader.ReadLine();
if (LineStr.Contains("${unreal_mono_pkg_path}"))
{
LineStr = LineStr.Replace("${unreal_mono_pkg_path}", InMonoPath);
}
if (LineStr.Contains("${unreal_mono_pkg_path_base}"))
{
LineStr = LineStr.Replace("${unreal_mono_pkg_path_base}", InMonoPathParent);
}
outputContents.Append(LineStr + '\n');
}
}
if (outputContents.Length > 0)
{
// Save the file. We Copy the template file to keep any permissions set to it.
try
{
Directory.CreateDirectory(Path.GetDirectoryName(InOutputFile));
if (File.Exists(InOutputFile))
{
File.SetAttributes(InOutputFile, File.GetAttributes(InOutputFile) & ~FileAttributes.ReadOnly);
File.Delete(InOutputFile);
}
File.Copy(InTemplateFile, InOutputFile);
File.SetAttributes(InOutputFile, File.GetAttributes(InOutputFile) & ~FileAttributes.ReadOnly);
using (var CmdFile = File.Open(InOutputFile, FileMode.OpenOrCreate | FileMode.Truncate))
{
Byte[] BytesToWrite = new UTF8Encoding(true).GetBytes(outputContents.ToString());
CmdFile.Write(BytesToWrite, 0, BytesToWrite.Length);
}
}
catch (Exception)
{
// Unable to write to the project file.
}
}
}
// --------------------------------------------------------------------------------
// ArchiveCommand.Automation.cs
public override void GetFilesToArchive(ProjectParams Params, DeploymentContext SC)
{
if (SC.StageTargetConfigurations.Count != 1)
{
throw new AutomationException("iOS is currently only able to package one target configuration at a time, but StageTargetConfigurations contained {0} configurations", SC.StageTargetConfigurations.Count);
}
// copy to archive directory
string PackagePath = Path.Combine(Path.GetDirectoryName(Params.RawProjectPath.FullName), "Binaries", "HTML5");
string UE4GameBasename = Path.GetFileNameWithoutExtension(Params.GetProjectExeForPlatform(UnrealTargetPlatform.HTML5).ToString());
string ProjectBasename = Params.ShortProjectName;
string ProjectConfiguration = Params.ClientConfigsToBuild[0].ToString();
if (ProjectConfiguration != "Development")
{
UE4GameBasename += "-HTML5-" + ProjectConfiguration;
ProjectBasename += "-HTML5-" + ProjectConfiguration;
}
SC.ArchiveFiles(PackagePath, UE4GameBasename + ".wasm"); // MAIN game code
SC.ArchiveFiles(PackagePath, UE4GameBasename + ".js"); // MAIN js file (emscripten)
SC.ArchiveFiles(PackagePath, UE4GameBasename + ".js.symbols"); // symbols file
SC.ArchiveFiles(PackagePath, "Utility.js"); // utilities
SC.ArchiveFiles(PackagePath, ProjectBasename + ".data"); // DATA file
SC.ArchiveFiles(PackagePath, ProjectBasename + ".data.js"); // DATA file js driver (emscripten)
SC.ArchiveFiles(PackagePath, ProjectBasename + ".UE4.js"); // UE4 js file
SC.ArchiveFiles(PackagePath, ProjectBasename + ".css"); // UE4 css file
SC.ArchiveFiles(PackagePath, ProjectBasename + ".html"); // landing page.
// Archive HTML5 Server and a Readme.
SC.ArchiveFiles(CombinePaths(CmdEnv.LocalRoot, "Engine/Binaries/DotNET/"), "HTML5LaunchHelper.exe");
SC.ArchiveFiles(CombinePaths(CmdEnv.LocalRoot, "Engine/Build/HTML5/"), "Readme.txt");
SC.ArchiveFiles(PackagePath, "RunMacHTML5LaunchHelper.command");
SC.ArchiveFiles(PackagePath, ".htaccess");
if (Compressed)
{
SC.ArchiveFiles(PackagePath, UE4GameBasename + ".wasmgz");
SC.ArchiveFiles(PackagePath, UE4GameBasename + ".jsgz");
SC.ArchiveFiles(PackagePath, UE4GameBasename + ".js.symbolsgz");
SC.ArchiveFiles(PackagePath, "Utility.jsgz");
SC.ArchiveFiles(PackagePath, ProjectBasename + ".datagz");
SC.ArchiveFiles(PackagePath, ProjectBasename + ".data.jsgz");
SC.ArchiveFiles(PackagePath, ProjectBasename + ".UE4.jsgz");
SC.ArchiveFiles(PackagePath, ProjectBasename + ".cssgz");
}
else
{
// nuke old compressed files to prevent using stale files
File.Delete(UE4GameBasename + ".wasmgz");
File.Delete(UE4GameBasename + ".jsgz");
File.Delete(UE4GameBasename + ".js.symbolsgz");
File.Delete("Utility.jsgz");
File.Delete(ProjectBasename + ".datagz");
File.Delete(ProjectBasename + ".data.jsgz");
File.Delete(ProjectBasename + ".UE4.jsgz");
File.Delete(ProjectBasename + ".cssgz");
}
if (HTMLPakAutomation.CanCreateMapPaks(Params))
{
// find all paks.
string[] Files = Directory.GetFiles(Path.Combine(PackagePath, Params.ShortProjectName), "*", SearchOption.AllDirectories);
foreach(string PakFile in Files)
{
SC.ArchivedFiles.Add(PakFile, Path.GetFileName(PakFile));
}
}
UploadToS3(SC, ProjectBasename + ".html");
}
// --------------------------------------------------------------------------------
// RunProjectCommand.Automation.cs
public override IProcessResult RunClient(ERunOptions ClientRunFlags, string ClientApp, string ClientCmdLine, ProjectParams Params)
{
// look for browser
string BrowserPath = Params.Devices[0].Replace("HTML5@", "");
// open the webpage
Int32 ServerPort = 8000; // HTML5LaunchHelper default
var ConfigCache = UnrealBuildTool.ConfigCache.ReadHierarchy(ConfigHierarchyType.Engine, DirectoryReference.FromFile(Params.RawProjectPath), UnrealTargetPlatform.HTML5);
ConfigCache.GetInt32("/Script/HTML5PlatformEditor.HTML5TargetSettings", "DeployServerPort", out ServerPort); // LaunchOn via Editor or FrontEnd
string WorkingDirectory = Path.GetDirectoryName(ClientApp);
string url = Path.GetFileName(ClientApp) +".html";
// WARNING: splitting the following situation
// if cookonthefly is used: tell the browser to use the PROXY at DEFAULT_HTTP_FILE_SERVING_PORT
// leave the normal HTML5LaunchHelper port (ServerPort) alone -- otherwise it will collide with the PROXY port
if (ClientCmdLine.Contains("filehostip"))
{
url += "?cookonthefly=true";
Int32 ProxyPort = 41898; // DEFAULT_HTTP_FILE_SERVING_PORT
url = String.Format("http://localhost:{0}/{1}", ProxyPort, url);
}
else
{
url = String.Format("http://localhost:{0}/{1}", ServerPort, url);
}
// Check HTML5LaunchHelper source for command line args
var LowerBrowserPath = BrowserPath.ToLower();
var ProfileDirectory = Path.Combine(Utils.GetUserSettingDirectory().FullName, "UE4_HTML5", "user");
string BrowserCommandline = url;
if (LowerBrowserPath.Contains("chrome"))
{
ProfileDirectory = Path.Combine(ProfileDirectory, "chrome");
// removing [--enable-logging] otherwise, chrome breaks with a bunch of the following errors:
// > ERROR:process_info.cc(631)] range at 0x7848406c00000000, size 0x1a4 fully unreadable
// leaving this note here for future reference: UE-45078
BrowserCommandline += " " + String.Format("--user-data-dir=\\\"{0}\\\" --no-first-run", ProfileDirectory);
}
else if (LowerBrowserPath.Contains("firefox"))
{
ProfileDirectory = Path.Combine(ProfileDirectory, "firefox");
BrowserCommandline += " " + String.Format("-no-remote -profile \\\"{0}\\\"", ProfileDirectory);
}
else if (LowerBrowserPath.Contains("safari"))
{
// NOT SUPPORTED: cannot have a separate UE4 profile for safari
// -- this "can" be done with a different user (e.g. guest) account...
// (which is not a turn key solution that can be done within UE4)
// -- some have tried using symlinks to "mimic" this
// https://discussions.apple.com/thread/3327990
// -- but, none of these are fool proof with an existing/running safari instance
// -- also, "Safari Extensions JS" has been officially deprecated as of Safari 12
// (in favor of using "Safari App Extension")
// https://developer.apple.com/documentation/safariextensions
// this means, Safari "LaunchOn" (UE4 Editor -> Launch -> Safari) will run with your FULL
// Safari profile -- so, all of your "previously opened" tabs will all also be opened...
}
// TODO: test on other platforms to remove this first if() check
if (UnrealBuildTool.BuildHostPlatform.Current.Platform == UnrealTargetPlatform.Linux)
{
if (!Directory.Exists(ProfileDirectory))
{
Directory.CreateDirectory(ProfileDirectory);
}
}
string LauncherArguments = string.Format(" -Browser=\"{0}\" + -BrowserCommandLine=\"{1}\" -ServerPort=\"{2}\" -ServerRoot=\"{3}\" ",
new object[] { BrowserPath, BrowserCommandline, ServerPort, WorkingDirectory });
var LaunchHelperPath = CombinePaths(CmdEnv.LocalRoot, "Engine/Binaries/DotNET/HTML5LaunchHelper.exe");
IProcessResult BrowserProcess = Run(LaunchHelperPath, LauncherArguments, null, ClientRunFlags | ERunOptions.NoWaitForExit);
return BrowserProcess;
}
public override List<FileReference> GetExecutableNames(DeploymentContext SC)
{
List<FileReference> ExecutableNames = new List<FileReference>();
ExecutableNames.Add(FileReference.Combine(SC.ProjectRoot, "Binaries", "HTML5", SC.ShortProjectName));
return ExecutableNames;
}
// --------------------------------------------------------------------------------
// PackageCommand.Automation.cs
public override bool RequiresPackageToDeploy
{
get { return true; }
}
// --------------------------------------------------------------------------------
// CookCommand.Automation.cs
public override string GetCookPlatform(bool bDedicatedServer, bool bIsClientOnly)
{
return "HTML5";
}
public override string GetCookExtraCommandLine(ProjectParams Params)
{
return HTMLPakAutomation.CanCreateMapPaks(Params) ? " -GenerateDependenciesForMaps " : "";
}
// --------------------------------------------------------------------------------
// CopyBuildToStagingDirectory.Automation.cs
public override void GetFilesToDeployOrStage(ProjectParams Params, DeploymentContext SC)
{
// must implement -- "empty" here
}
public override PakType RequiresPak(ProjectParams Params)
{
return HTMLPakAutomation.CanCreateMapPaks(Params) ? PakType.Never : PakType.Always;
}
public override string GetPlatformPakCommandLine(ProjectParams Params, DeploymentContext SC)
{
return Compressed ? " -compress" : "";
}
// --------------------------------------------------------------------------------
// AutomationUtils/Platform.cs
public override bool IsSupported { get { return true; } }
// --------------------------------------------------------------------------------
// --------------------------------------------------------------------------------
#region AMAZON S3
public void UploadToS3(DeploymentContext SC, string OutputFilename)
{
ConfigHierarchy Ini = ConfigCache.ReadHierarchy(ConfigHierarchyType.Engine, DirectoryReference.FromFile(SC.RawProjectPath), SC.StageTargetPlatform.PlatformType);
bool Upload = false;
string Region = "";
string KeyId = "";
string AccessKey = "";
string BucketName = "";
string FolderName = "";
if (! Ini.GetBool("/Script/HTML5PlatformEditor.HTML5TargetSettings", "UploadToS3", out Upload) || ! Upload )
{
return;
}
bool AmazonIdentity = Ini.GetString("/Script/HTML5PlatformEditor.HTML5TargetSettings", "S3Region", out Region) &&
Ini.GetString("/Script/HTML5PlatformEditor.HTML5TargetSettings", "S3KeyID", out KeyId) &&
Ini.GetString("/Script/HTML5PlatformEditor.HTML5TargetSettings", "S3SecretAccessKey", out AccessKey) &&
Ini.GetString("/Script/HTML5PlatformEditor.HTML5TargetSettings", "S3BucketName", out BucketName);
if ( !AmazonIdentity )
{
LogInformation("Amazon S3 Incorrectly configured");
return;
}
Ini.GetString("/Script/HTML5PlatformEditor.HTML5TargetSettings", "S3FolderName", out FolderName);
if ( FolderName == "" )
{
FolderName = SC.ShortProjectName;
}
else
{
// strip any before and after folder "/"
FolderName = Regex.Replace(Regex.Replace(FolderName, "^/+", "" ), "/+$", "");
}
List<Task> UploadTasks = new List<Task>();
long msTimeOut = 0;
foreach (KeyValuePair<string, string> Entry in SC.ArchivedFiles)
{
FileInfo Info = new FileInfo(Entry.Key);
UploadTasks.Add(UploadToS3Worker(Info, Region, KeyId, AccessKey, BucketName, FolderName));
if ( msTimeOut < Info.Length )
{
msTimeOut = Info.Length;
}
}
msTimeOut /= 100; // [miliseconds] give 10 secs for each ~MB ( (10s * 1000ms) / ( 1024KB * 1024MB * 1000ms ) )
if ( msTimeOut < (100*1000) ) // HttpClient: default timeout is 100 sec
{
msTimeOut = 100*1000;
}
LogInformation("Upload Timeout set to: " + (msTimeOut/1000) + "secs");
Task.WaitAll(UploadTasks.ToArray(), (int)msTimeOut); // set timeout [miliseconds]
string URL = "https://" + BucketName + ".s3.amazonaws.com/" + FolderName + "/" + OutputFilename;
LogInformation("Your project's shareable link is: " + URL);
LogInformation("Upload Tasks finished.");
}
private static IDictionary<string, string> MimeTypeMapping = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
{
// the following will default to: "appication/octet-stream"
// .data .datagz
{ ".wasm", "application/wasm" },
{ ".wasmgz", "application/wasm" },
{ ".htaccess", "text/plain"},
{ ".html", "text/html"},
{ ".css", "text/css"},
{ ".cssgz", "text/css"},
{ ".js", "application/x-javascript" },
{ ".jsgz", "application/x-javascript" },
{ ".symbols", "text/plain"},
{ ".symbolsgz", "text/plain"},
{ ".txt", "text/plain"}
};
static async Task UploadToS3Worker(FileInfo Info, string Region, string KeyId, string AccessKey, string BucketName, string FolderName)
{
// --------------------------------------------------
// "AWS Signature Version 4"
// http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
// --------------------------------------------------
LogInformation(" Uploading " + Info.Name);
// --------------------------------------------------
// http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-post-example.html
string TimeStamp = DateTime.UtcNow.ToString("yyyyMMddTHHmmssZ");
string DateExpire = DateTime.UtcNow.AddDays(1).ToString("yyyy-MM-dd");
string AWSDate = DateTime.UtcNow.AddDays(1).ToString("yyyyMMdd");
string MimeType = (MimeTypeMapping.ContainsKey(Info.Extension))
? MimeTypeMapping[Info.Extension]
: "application/octet-stream";
string MimePath = MimeType.Split('/')[0];
string AWSCredential = KeyId + "/" + AWSDate + "/" + Region + "/s3/aws4_request";
// --------------------------------------------------
string policy = "{ \"expiration\": \"" + DateExpire + "T12:00:00.000Z\"," +
" \"conditions\": [" +
" { \"bucket\": \"" + BucketName + "\" }," +
" [ \"starts-with\", \"$key\", \"" + FolderName + "/\" ]," +
" { \"acl\": \"public-read\" }," +
" [ \"starts-with\", \"$content-type\", \"" + MimePath + "/\" ],";
if (Info.Extension.EndsWith("gz"))
{
policy += " [ \"starts-with\", \"$content-encoding\", \"gzip\" ],";
}
policy += " { \"x-amz-credential\": \"" + AWSCredential + "\" }," +
" { \"x-amz-algorithm\": \"AWS4-HMAC-SHA256\" }," +
" { \"x-amz-date\": \"" + TimeStamp + "\" }" +
" ]" +
"}";
string policyBase64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(policy), Base64FormattingOptions.InsertLineBreaks);
// --------------------------------------------------
// http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html
var kha = KeyedHashAlgorithm.Create("HmacSHA256");
kha.Key = Encoding.UTF8.GetBytes(("AWS4" + AccessKey).ToCharArray()); // kSecret
byte[] sig = kha.ComputeHash(Encoding.UTF8.GetBytes(AWSDate));
kha.Key = sig; // kDate
sig = kha.ComputeHash(Encoding.UTF8.GetBytes(Region));
kha.Key = sig; // kRegion
sig = kha.ComputeHash(Encoding.UTF8.GetBytes("s3"));
kha.Key = sig; // kService
sig = kha.ComputeHash(Encoding.UTF8.GetBytes("aws4_request"));
kha.Key = sig; // kSigning
sig = kha.ComputeHash(Encoding.UTF8.GetBytes(policyBase64));
string signature = BitConverter.ToString(sig).Replace("-", "").ToLower(); // for Authorization
// debugging...
//Console.WriteLine("policy: [" + policy + "]");
//Console.WriteLine("policyBase64: [" + policyBase64 + "]");
//Console.WriteLine("signature: [" + signature + "]");
// --------------------------------------------------
HttpClient httpClient = new HttpClient();
var formData = new MultipartFormDataContent();
formData.Add(new StringContent(FolderName + "/" + Info.Name), "key");
formData.Add(new StringContent("public-read"), "acl");
formData.Add(new StringContent(AWSCredential), "X-Amz-Credential");
formData.Add(new StringContent("AWS4-HMAC-SHA256"), "X-Amz-Algorithm");
formData.Add(new StringContent(signature), "X-Amz-Signature");
formData.Add(new StringContent(TimeStamp), "X-Amz-Date");
formData.Add(new StringContent(policyBase64), "Policy");
formData.Add(new StringContent(MimeType), "Content-Type");
if ( Info.Extension.EndsWith("gz") )
{
formData.Add(new StringContent("gzip"), "Content-Encoding");
}
// debugging...
//Console.WriteLine("key: [" + FolderName + "/" + Info.Name + "]");
//Console.WriteLine("AWSCredential: [" + AWSCredential + "]");
//Console.WriteLine("TimeStamp: [" + TimeStamp + "]");
//Console.WriteLine("MimeType: [" + MimeType + "]");
// the file ----------------------------------------
var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(Info.FullName));
fileContent.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse(MimeType);
formData.Add(fileContent, "file", Info.Name);
int adjustTimeout = (int)(Info.Length / (100*1000)); // [seconds] give 10 secs for each ~MB ( (10s * 1000ms) / ( 1024KB * 1024MB * 1000ms ) )
if ( adjustTimeout > 100 ) // default timeout is 100 sec
{
httpClient.Timeout = TimeSpan.FromSeconds(adjustTimeout); // increase timeout
}
//Console.WriteLine("httpClient Timeout: [" + httpClient.Timeout + "]" );
// upload ----------------------------------------
string URL = "https://" + BucketName + ".s3.amazonaws.com/";
var response = await httpClient.PostAsync(URL, formData);
if (response.IsSuccessStatusCode)
{
LogInformation("Upload done: " + Info.Name);
}
else
{
var contents = response.Content.ReadAsStringAsync();
var reason = Regex.Replace(
/* grab inner block */ Regex.Replace(contents.Result, "<[^>]+>\n<[^>]+>([^<]+)</[^>]+>", "$1")
/* strip tags */ , "<([^>]+)>([^<]+)</[^>]+>", "$1 - $2\n");
//Console.WriteLine("Fail to Upload: " + Info.Name + " Header - " + response.ToString());
Console.WriteLine("Fail to Upload: " + Info.Name + "\nResponse - " + reason);
throw new Exception("FAILED TO UPLOAD: " + Info.Name);
}
}
#endregion
}