Skip to content

Commit

Permalink
Play .ogv file if .mp4 file missing on Windows
Browse files Browse the repository at this point in the history
 - Required to fix video playback on Proton/Wine on ch1-8 (see #78)
 - Note: ch9 (rei) implements video playback differently, so these changes should not be applied to ch9
  • Loading branch information
drojf committed Sep 21, 2022
1 parent 1b55987 commit ff694a5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion MOD.Scripts.Core.Movie/AVProMovieRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void Init(MovieInfo movieInfo)
mediaPlayer.m_AutoOpen = true;
mediaPlayer.m_AutoStart = true;
mediaPlayer.m_Volume = movieInfo.Volume;
mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, movieInfo.Path + ".mp4");
mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, movieInfo.PathWithExt);
MODApplyToMaterial mODApplyToMaterial = base.gameObject.AddComponent<MODApplyToMaterial>();
mODApplyToMaterial._material = movieInfo.Layer.MODMaterial;
mODApplyToMaterial._texturePropertyName = "_Primary";
Expand Down
12 changes: 10 additions & 2 deletions MOD.Scripts.Core.Movie/MovieInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ namespace MOD.Scripts.Core.Movie
{
public class MovieInfo
{
public static string GetPathFromNameWithExt(string name, string ext)
{
return System.IO.Path.Combine(Application.streamingAssetsPath, "movies/" + name + ext);
}

private readonly string Ext;

public string Name
{
get;
Expand All @@ -16,13 +23,14 @@ public string Name

public float Volume => Math.Min(1f, 1.55f * GameSystem.Instance.AudioController.BGMVolume);

public string Path => "file:///" + System.IO.Path.Combine(Application.streamingAssetsPath, "movies/" + Name);
public string PathWithExt => "file:///" + GetPathFromNameWithExt(Name, Ext);

public Layer Layer => GameSystem.Instance.SceneController.MODActiveScene.BackgroundLayer;

public MovieInfo(string name)
public MovieInfo(string name, string ext)
{
Name = name;
Ext = ext;
}
}
}
2 changes: 1 addition & 1 deletion MOD.Scripts.Core.Movie/TextureMovieRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public TextureMovieRenderer()
public void Init(MovieInfo movieInfo)
{
this.movieInfo = movieInfo;
www = new WWW(movieInfo.Path + ".ogv");
www = new WWW(movieInfo.PathWithExt);
audioSource = base.gameObject.AddComponent<AudioSource>();
audioSource.volume = this.movieInfo.Volume;
base.enabled = true;
Expand Down
47 changes: 36 additions & 11 deletions MOD.Scripts.Core.State/StateMovie.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
using Assets.Scripts.Core;
using Assets.Scripts.Core.State;
using MOD.Scripts.Core.Movie;
using MOD.Scripts.UI;
using System.IO;
using UnityEngine;

namespace MOD.Scripts.Core.State
{
public class StateMovie : IGameState
{
const string windowsMovieExtension = ".mp4";
const string linuxMovieExtension = ".ogv";

private bool isLeaving;

private IMovieRenderer MovieEntity;
Expand All @@ -17,10 +22,39 @@ public class StateMovie : IGameState

public StateMovie(string moviename)
{
movieInfo = new MovieInfo(moviename);
// Windows and Wine/Proton will both show up as WindowsPlayer
bool isWindowsOrWine = Application.platform == RuntimePlatform.WindowsPlayer;

// Only play the .mp4 file with AVProVideo on Windows-like Platforms, if the file exists
// On Wine, it is expected that only the Linux .ogv video file is installed, as playing using AVProVideo/.mp4 files on Wine is not supported
bool windowsPlaybackMode = isWindowsOrWine && File.Exists(MovieInfo.GetPathFromNameWithExt(moviename, windowsMovieExtension));

// The below just shows a message if no video file found to play (it does not affect playback behavior)
if (!windowsPlaybackMode && !File.Exists(MovieInfo.GetPathFromNameWithExt(moviename, linuxMovieExtension)))
{
string movieFiles = $"{moviename}{linuxMovieExtension}";
if(isWindowsOrWine)
{
movieFiles = $"{moviename}{windowsMovieExtension} or " + movieFiles;
}

string errorMessage = $"ERROR: Movie file {movieFiles} not found";
Debug.Log(errorMessage);
MODToaster.Show(errorMessage, toastDuration:10);
}

movieInfo = new MovieInfo(moviename, windowsPlaybackMode ? windowsMovieExtension : linuxMovieExtension);
gameObject = new GameObject();
SetupBackgroundLayerForVideo();
MovieEntity = CreateMovieRenderer();

if(windowsPlaybackMode)
{
MovieEntity = gameObject.AddComponent<AVProMovieRenderer>();
}
else
{
MovieEntity = gameObject.AddComponent<TextureMovieRenderer>();
}
MovieEntity.Init(movieInfo);
}

Expand Down Expand Up @@ -76,15 +110,6 @@ public void Leave()
}
}

private IMovieRenderer CreateMovieRenderer()
{
if (Application.platform == RuntimePlatform.WindowsPlayer)
{
return gameObject.AddComponent<AVProMovieRenderer>();
}
return gameObject.AddComponent<TextureMovieRenderer>();
}

private void SetupBackgroundLayerForVideo()
{
movieInfo.Layer.ReleaseTextures();
Expand Down

0 comments on commit ff694a5

Please sign in to comment.