Files
cruiseSimulator/Assets/BigShipTycoon/Runtime/PortClipExport.cs

88 lines
3.6 KiB
C#

using System.Collections;
using System.IO;
using UnityEngine;
namespace BigShipTycoon
{
public sealed partial class TycoonApp
{
private const int PortClipFrameRate = 24;
private bool holdCinematicClock;
private IEnumerator ExportPortClips(string output)
{
Directory.CreateDirectory(output);
Time.captureFramerate = PortClipFrameRate;
holdCinematicClock = true;
StartCompany(false);
yield return null;
if (company.Permit(2) is string permitFailure)
throw new IOException(permitFailure);
foreach (int destination in new[] { 1, 2, 0 })
{
Voyage quote = company.Forecast(Ship, destination);
if (Ship.Fuel < quote.RequiredFuel && company.Refuel(Ship, quote.RequiredFuel - Ship.Fuel) is string fuelFailure)
throw new IOException(fuelFailure);
string origin = Market.Ports[Ship.Port].Name.Replace(" ", "");
string arrival = Market.Ports[destination].Name.Replace(" ", "");
BeginVoyage(company.Forecast(Ship, destination));
yield return WritePortFrames(output, "Departure-" + origin);
EndPortCinematic();
FinishVoyage();
yield return WritePortFrames(output, "Arrival-" + arrival);
EndPortCinematic();
}
Time.captureFramerate = 0;
holdCinematicClock = false;
Debug.Log("Big Ship Tycoon port clips exported to " + output);
Application.Quit(0);
}
private IEnumerator WritePortFrames(string output, string name)
{
if (!harbor.PortCinematicActive)
throw new IOException("No port cinematic started for " + name);
float step = 1f / PortClipFrameRate;
int frames = Mathf.CeilToInt(Harbor.PortCinematicDuration * PortClipFrameRate);
using var stream = File.Create(Path.Combine(output, name + ".raw"));
var rendered = new WaitForEndOfFrame();
int width = Screen.width, height = Screen.height;
var target = new RenderTexture(width, height, 0, RenderTextureFormat.ARGB32);
var image = new Texture2D(width, height, TextureFormat.RGBA32, false);
var bytes = new byte[width * height * 4];
try
{
File.WriteAllText(Path.Combine(output, name + ".txt"), $"{width} {height} {image.format} {PortClipFrameRate}");
for (int frame = 0; frame < frames; frame++)
{
cinematicTime = frame * step;
PresentPortCinematicFrame(step);
yield return rendered;
ScreenCapture.CaptureScreenshotIntoRenderTexture(target);
RenderTexture previous = RenderTexture.active;
try
{
RenderTexture.active = target;
image.ReadPixels(new Rect(0, 0, width, height), 0, 0, false);
}
finally
{
RenderTexture.active = previous;
}
image.GetRawTextureData<byte>().CopyTo(bytes);
stream.Write(bytes, 0, bytes.Length);
yield return null;
}
}
finally
{
target.Release();
Destroy(target);
Destroy(image);
}
}
}
}