Survivalcraft API 1.8.2.3 v1.8.2.3
Survivalcraft 2.4
载入中...
搜索中...
未找到
ScreenCaptureManager.cs
浏览该文件的文档.
1using Engine;
3using Engine.Media;
4#if ANDROID
5using Android.Content;
6#endif
7
8namespace Game {
9 public static class ScreenCaptureManager {
10 public static readonly string ScreenshotDir = "app:/ScreenCapture";
11
12 public static bool m_captureRequested;
13
14 public static Action m_successHandler;
15
16 public static Action<Exception> m_failureHandler;
17
18 public static void Run() {
20 try {
21 int width;
22 int height;
24 case ScreenshotSize.ScreenSize: {
25 width = Math.Max(Window.ScreenSize.X, Window.ScreenSize.Y);
26 height = Math.Min(Window.ScreenSize.X, Window.ScreenSize.Y);
27 float num2 = width / (float)height;
28 width = Math.Min(width, 2048);
29 height = (int)MathF.Round(width / num2);
30 break;
31 }
32 case ScreenshotSize.FullHD:
33 if (Display.MaxTextureSize < 3840) {
34 width = Display.MaxTextureSize;
35 height = (int)MathF.Round(width * 9f / 16f);
36 }
37 else {
38 width = 3840;
39 height = 2160;
40 }
41 break;
42 case ScreenshotSize.Custom:
45 break;
46 default:
47 width = 1920;
48 height = 1080;
49 break;
50 }
51 DateTime now = DateTime.Now;
52 Capture(
53 width,
54 height,
55 $"Survivalcraft {now.Year:D4}-{now.Month:D2}-{now.Day:D2} {now.Hour:D2}-{now.Minute:D2}-{now.Second:D2}.png"
56 );
57 m_successHandler?.Invoke();
58 GC.Collect();
59 }
60 catch (Exception ex) {
61 Log.Error($"Error capturing screen. Reason: {ex.Message}");
62 m_failureHandler?.Invoke(ex);
63 }
64 finally {
65 m_captureRequested = false;
66 m_successHandler = null;
67 m_failureHandler = null;
68 }
69 }
70 }
71
72 public static void CapturePhoto(Action success, Action<Exception> failure) {
73 if (!m_captureRequested) {
74 m_captureRequested = true;
75 m_successHandler = success;
76 m_failureHandler = failure;
77 }
78 }
79
80 public static void Capture(int width, int height, string filename) {
81#if ANDROID
82#pragma warning disable CA1416
83 if (Android.OS.Build.VERSION.SdkInt < (Android.OS.BuildVersionCodes)21) {
84 return;
85 }
86#endif
87 if (GameManager.Project != null) {
88 using (RenderTarget2D renderTarget2D = new(width, height, 1, ColorFormat.Rgba8888, DepthFormat.Depth24Stencil8)) {
89 RenderTarget2D renderTarget = Display.RenderTarget;
90 Dictionary<ComponentGui, bool> dictionary = new();
91 ResolutionMode resolutionMode = ResolutionMode.High;
92 try {
94 foreach (ComponentPlayer componentPlayer in GameManager.Project.FindSubsystem<SubsystemPlayers>(true).ComponentPlayers) {
95 dictionary[componentPlayer.ComponentGui] = componentPlayer.ComponentGui.ControlsContainerWidget.IsVisible;
96 componentPlayer.ComponentGui.ControlsContainerWidget.IsVisible = false;
97 }
98 }
99 resolutionMode = SettingsManager.ResolutionMode;
101 Display.RenderTarget = renderTarget2D;
104 PrimitivesRenderer2D primitivesRenderer2D = new();
105 Texture2D texture2D = ContentManager.Get<Texture2D>("Textures/Gui/ScreenCaptureOverlay");
106 Vector2 vector = new((width - texture2D.Width) / 2, 0f);
107 Vector2 corner = vector + new Vector2(texture2D.Width, texture2D.Height);
108 primitivesRenderer2D.TexturedBatch(texture2D, false, 0, DepthStencilState.None)
109 .QueueQuad(vector, corner, 0f, new Vector2(0f, 0f), new Vector2(1f, 1f), Color.White);
110 primitivesRenderer2D.Flush();
111 }
112 }
113 finally {
114 Display.RenderTarget = renderTarget;
115 foreach (KeyValuePair<ComponentGui, bool> item in dictionary) {
116 item.Key.ControlsContainerWidget.IsVisible = item.Value;
117 }
118 SettingsManager.ResolutionMode = resolutionMode;
119 }
120#if !ANDROID
123 }
124 using (Stream stream = Storage.OpenFile(Storage.CombinePaths(ScreenshotDir, filename), OpenFileMode.CreateOrOpen)) {
125 Image.Save(
126 renderTarget2D.GetData(new Rectangle(0, 0, renderTarget2D.Width, renderTarget2D.Height)),
127 stream,
128 renderTarget2D.Width * renderTarget2D.Height <= 8294400 ? ImageFileFormat.Png : ImageFileFormat.Bmp,
129 false
130 );
131 }
132#else
133 string path = Storage.CombinePaths(ModsManager.ScreenCapturePath, filename);
136 }
137 using (Stream stream = Storage.OpenFile(path, OpenFileMode.CreateOrOpen)) {
138 Image.Save(
139 renderTarget2D.GetData(new Rectangle(0, 0, renderTarget2D.Width, renderTarget2D.Height)),
140 stream,
141 renderTarget2D.Width * renderTarget2D.Height <= 8294400 ? ImageFileFormat.Png : ImageFileFormat.Bmp,
142 false
143 );
144 }
145 Intent intent = new("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
146 intent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(Storage.GetSystemPath(path))));
147 Window.Activity.SendBroadcast(intent);
148#pragma warning restore CA1416
149#endif
150 }
152 "OnCapture",
153 loader => {
154 loader.OnCapture();
155 return false;
156 }
157 );
158 }
159 }
160 }
161}
162//修复Image问题后将54、129、136行修改为WebP
static readonly DepthStencilState None
static RenderTarget2D RenderTarget
TexturedBatch2D TexturedBatch(Texture2D texture, bool useAlphaTest=false, int layer=0, DepthStencilState depthStencilState=null, RasterizerState rasterizerState=null, BlendState blendState=null, SamplerState samplerState=null)
void Flush(bool clearAfterFlush=true, int maxLayer=int.MaxValue)
unsafe Image GetData(Rectangle sourceRectangle)
void QueueQuad(Vector2 corner1, Vector2 corner2, float depth, Vector2 texCoord1, Vector2 texCoord2, Color color)
static void Error(object message)
定义 Log.cs:80
static void Save(Image image, Stream stream, ImageFileFormat format, bool saveAlpha, bool sync=false)
static string GetSystemPath(string path)
static void CreateDirectory(string path)
static bool DirectoryExists(string path)
static Stream OpenFile(string path, OpenFileMode openFileMode)
static string CombinePaths(params string[] paths)
static Point2 ScreenSize
ContainerWidget ControlsContainerWidget
static object Get(Type type, string name)
static Project Project
static void Capture(int width, int height, string filename)
static Action< Exception > m_failureHandler
static void CapturePhoto(Action success, Action< Exception > failure)
static int[] ScreenshotSizeCustomWidths
static ScreenshotSize ScreenshotSize
static float[] ScreenshotSizeCustomAspectRatios
static ResolutionMode ResolutionMode
static int ScreenshotSizeCustomAspectRatioIndex
ReadOnlyList< ComponentPlayer > ComponentPlayers
virtual bool IsVisible
static void HookAction(string HookName, Func< ModLoader, bool > action)
执行Hook
static string ScreenCapturePath
static Color White