Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General changes to depend on interfaces instead of concrete classes #17

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 0 additions & 115 deletions OBS.cs

This file was deleted.

15 changes: 15 additions & 0 deletions OBS/IOBSManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Threading.Tasks;

namespace PowerPointToOBSSceneSwitcher.OBS
{
public interface IOBSManager : IDisposable
{
public String DefaultScene { get; set; }
Task Connect();
void Init();
bool ChangeScene(string sceneName);
void StartRecording();
void StopRecording();
}
}
133 changes: 133 additions & 0 deletions OBS/OBSManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using OBS.WebSocket.NET;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PowerPointToOBSSceneSwitcher.OBS
{
public class OBSManager : IOBSManager
{
private ObsWebSocket _OBS;
private List<string> _validScenes;
private bool _DisposedValue;
private string _defaultScene;
private string _currentScene;

public OBSManager(){ }

public string DefaultScene
{
get { return _defaultScene; }
set
{
if (_validScenes.Contains(value))
{
_defaultScene = value;
Console.WriteLine($" Setting the default OBS Scene to \"{value}\"");
}
else
{
Console.WriteLine($"Scene named {value} does not exist and cannot be set as default");
}
}
}

public Task Connect()
{
_OBS = new ObsWebSocket();
_OBS.Connect($"ws://127.0.0.1:4444", "");
return Task.CompletedTask;
}

public void Init()
{
GetScenes();
}

private void GetScenes()
{
var allScene = _OBS.Api.GetSceneList();
var list = allScene.Scenes.Select(s => s.Name).ToList();
Console.WriteLine("Valid Scenes:");
foreach (var l in list)
{
Console.WriteLine(l);
}
_validScenes = list;
_defaultScene = _validScenes.FirstOrDefault();
}

public bool ChangeScene(string sceneName)
{
if (!_validScenes.Contains(sceneName))
{
Console.WriteLine($"Scene named {sceneName} does not exist");
if (String.IsNullOrEmpty(_defaultScene))
{
Console.WriteLine("No default scene has been set!");
return false;
}

sceneName = _defaultScene;
}

if(_currentScene != sceneName)
{
Console.WriteLine($" Switching to OBS Scene named \"{sceneName}\"");
_OBS.Api.SetCurrentScene(sceneName);
_currentScene = sceneName;
}

return true;
}

public void StartRecording()
{
try
{
_OBS.Api.StartRecording();
Console.WriteLine($"Recording Started");
}
catch { /* Recording already started */ }
}

public void StopRecording()
{
try
{
_OBS.Api.StopRecording();
Console.WriteLine($"Recording Stopped");
}
catch { /* Recording already stopped */ }
}

~OBSManager()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (!_DisposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
}

_OBS.Disconnect();
_OBS = null;
_DisposedValue = true;
}
}
}
}
47 changes: 47 additions & 0 deletions PowerPointSwitcherController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.Office.Interop.PowerPoint;
using PowerPointToOBSSceneSwitcher.OBS;
using PowerPointToOBSSceneSwitcher.SceneManagers;
using System;
using System.Threading.Tasks;

namespace PowerPointToOBSSceneSwitcher
{
public class PowerPointSwitcherController
{
private readonly Application _ppt;
private readonly IOBSManager _obsManager;

public PowerPointSwitcherController(IOBSManager obsManager)
{
_ppt = new Application();
_obsManager = obsManager;
}

public async Task Connect()
{
Console.Write("Connecting to PowerPoint...");
_ppt.SlideShowNextSlide += App_SlideShowNextSlide;
Console.WriteLine("connected");

Console.Write("Connecting to OBS...");
await _obsManager.Connect();
Console.WriteLine("connected");
_obsManager.Init();
}

private void App_SlideShowNextSlide(SlideShowWindow Wn)
{
if (Wn != null)
{
Console.WriteLine($"Moved to Slide Number {Wn.View.Slide.SlideNumber}");
//Text starts at Index 2 ¯\_(ツ)_/¯
var note = String.Empty;
try { note = Wn.View.Slide.NotesPage.Shapes[2].TextFrame.TextRange.Text; }
catch { /*no notes*/ }

ISceneManager reader = new PowerpointNotesSceneManager(_obsManager);
reader.ProcessData(note);
}
}
}
}
Loading