├── .gitattributes ├── .gitignore ├── CefUnityLib ├── CefController.cs ├── CefUnityLib.csproj ├── Consts.cs ├── Helpers │ ├── Keys.cs │ └── MouseButtons.cs ├── Messages │ ├── KeyEventPipeMessage.cs │ ├── MouseEventPipeMessage.cs │ └── MouseWheelEventPipeMessage.cs ├── PipeProto.cs ├── PipeProtoMessage.cs └── Properties │ └── AssemblyInfo.cs ├── CefUnityServer.sln ├── CefUnityServer ├── App.config ├── BrowserHost.cs ├── CefUnityServer.csproj ├── ITaskRunnable.cs ├── Logr.cs ├── PipeServer.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── TaskRunner.cs ├── Tasks │ ├── RepaintFrameTask.cs │ ├── SendFrameTask.cs │ ├── SendKeyEventTask.cs │ ├── SendMouseWheelEventTask.cs │ ├── SetMouseTask.cs │ └── ShutdownTask.cs └── packages.config ├── CefUnityTestClient ├── App.config ├── CefUnityTestClient.csproj ├── Form1.Designer.cs ├── Form1.cs ├── Form1.resx ├── Program.cs └── Properties │ ├── AssemblyInfo.cs │ ├── Resources.Designer.cs │ ├── Resources.resx │ ├── Settings.Designer.cs │ └── Settings.settings ├── LICENSE ├── README.md ├── SampleUnityIntegration ├── CefIntegrationBehavior.cs ├── Properties │ └── AssemblyInfo.cs └── SampleUnityIntegration.csproj ├── screenshot-ingame-canvas.png ├── screenshot-test-client.png └── screenshot-volume-mixer.png /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /CefUnityLib/CefController.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib.Helpers; 2 | using CefUnityLib.Messages; 3 | using System; 4 | using System.Collections; 5 | using System.Collections.Generic; 6 | using System.Diagnostics; 7 | using System.IO; 8 | using System.IO.Pipes; 9 | using System.Linq; 10 | using System.Text; 11 | using System.Threading.Tasks; 12 | 13 | namespace CefUnityLib 14 | { 15 | public class CefController 16 | { 17 | protected string pipeName; 18 | protected NamedPipeClientStream stream; 19 | protected StreamReader reader; 20 | protected StreamWriter writer; 21 | 22 | public EventHandler MessageReceived; 23 | public EventHandler ConnectionStateChanged; 24 | 25 | protected ulong statMsgsReceived = 0; 26 | protected ulong statMsgsSent = 0; 27 | 28 | public ulong MessagesReceivedCount 29 | { 30 | get 31 | { 32 | return statMsgsReceived; 33 | } 34 | } 35 | 36 | public ulong MessagesSentCount 37 | { 38 | get 39 | { 40 | return statMsgsSent; 41 | } 42 | } 43 | 44 | public string PipeName 45 | { 46 | get 47 | { 48 | return pipeName; 49 | } 50 | } 51 | 52 | public bool Connected 53 | { 54 | get 55 | { 56 | return stream != null && stream.IsConnected; 57 | } 58 | } 59 | 60 | public CefController(string pipeName = "default") 61 | { 62 | // Auto prefix pipe name 63 | if (!pipeName.StartsWith(Consts.NAMED_PIPE_PREFIX)) 64 | { 65 | pipeName = Consts.NAMED_PIPE_PREFIX + pipeName; 66 | } 67 | 68 | this.pipeName = pipeName; 69 | } 70 | 71 | public bool SendMessage(PipeProtoMessage msg) 72 | { 73 | if (Connected) 74 | { 75 | try 76 | { 77 | msg.WriteToClientStream(stream, true); 78 | statMsgsSent++; 79 | } 80 | catch (Exception ex) 81 | { 82 | Debug.WriteLine("Send message failure: " + ex.Message + " " + ex.StackTrace.ToString()); 83 | 84 | if (Debugger.IsAttached) 85 | { 86 | Debugger.Break(); 87 | } 88 | 89 | return false; 90 | } 91 | 92 | return true; 93 | } 94 | 95 | return false; 96 | } 97 | 98 | public void SendMouseEvent(byte eventType, Int32 coordX, Int32 coordY, MouseButtons mouseButtons) 99 | { 100 | SendMessage(new MouseEventPipeMessage(eventType, coordX, coordY, mouseButtons)); 101 | } 102 | 103 | public void SendMouseWheelEvent(int x, int y, int delta) 104 | { 105 | SendMessage(new MouseWheelEventPipeMessage(x, y, delta)); 106 | } 107 | 108 | public void SendKeyEvent(byte eventType, Keys keys, Keys modifiers) 109 | { 110 | SendMessage(new KeyEventPipeMessage(eventType, keys, modifiers)); 111 | } 112 | 113 | public void SendKeyCharEvent(int keyCharCode, Keys modifiers = Keys.None) 114 | { 115 | SendMessage(new KeyEventPipeMessage(KeyEventPipeMessage.TYPE_KEY_CHAR, keyCharCode, modifiers)); 116 | } 117 | 118 | public void SendShutdownMessage() 119 | { 120 | SendMessage(new PipeProtoMessage 121 | { 122 | Opcode = PipeProto.OPCODE_SHUTDOWN 123 | }); 124 | } 125 | 126 | public void SendRepaintRequest() 127 | { 128 | SendMessage(new PipeProtoMessage 129 | { 130 | Opcode = PipeProto.OPCODE_FRAME 131 | }); 132 | } 133 | 134 | public void Disconnect() 135 | { 136 | if (stream != null) 137 | { 138 | try 139 | { 140 | stream.Dispose(); 141 | } 142 | catch (Exception) { } 143 | } 144 | 145 | stream = null; 146 | 147 | if (ConnectionStateChanged != null) 148 | { 149 | ConnectionStateChanged.Invoke(this, Connected); 150 | } 151 | } 152 | 153 | #pragma warning disable CS4014 154 | public void Connect() 155 | { 156 | if (Connected) 157 | { 158 | return; 159 | } 160 | 161 | stream = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut, PipeOptions.Asynchronous); 162 | 163 | try 164 | { 165 | stream.Connect(3000); 166 | } 167 | catch (Exception ex) 168 | { 169 | Disconnect(); 170 | 171 | if (ConnectionStateChanged != null) 172 | { 173 | ConnectionStateChanged.Invoke(this, Connected); 174 | } 175 | 176 | throw ex; 177 | } 178 | 179 | Task.Run(new Action(() => 180 | { 181 | if (ConnectionStateChanged != null) 182 | { 183 | ConnectionStateChanged.Invoke(this, Connected); 184 | } 185 | 186 | // Send ping message 187 | var pingMessage = new PipeProtoMessage 188 | { 189 | Opcode = PipeProto.OPCODE_PING 190 | }; 191 | 192 | try 193 | { 194 | pingMessage.WriteToClientStream(stream, true); 195 | statMsgsSent++; 196 | } 197 | catch (Exception ex) 198 | { 199 | Debug.WriteLine("Send message failure: " + ex.Message + " " + ex.StackTrace.ToString()); 200 | 201 | if (Debugger.IsAttached) 202 | { 203 | Debugger.Break(); 204 | } 205 | 206 | Disconnect(); 207 | } 208 | 209 | // Ask for a repaint 210 | SendRepaintRequest(); 211 | 212 | while (Connected) 213 | { 214 | // Read next message 215 | PipeProtoMessage incomingMessage = null; 216 | 217 | try 218 | { 219 | incomingMessage = PipeProtoMessage.ReadFromStream(stream); 220 | } 221 | catch (Exception) { } 222 | 223 | if (incomingMessage != null) 224 | { 225 | try 226 | { 227 | if (MessageReceived != null) 228 | { 229 | MessageReceived.Invoke(this, incomingMessage); 230 | statMsgsReceived++; 231 | } 232 | } 233 | catch (Exception) { } 234 | } 235 | } 236 | })); 237 | } 238 | #pragma warning restore CS4014 239 | } 240 | } -------------------------------------------------------------------------------- /CefUnityLib/CefUnityLib.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {483E28CB-9845-4AB7-BA58-13A579A155F7} 8 | Library 9 | Properties 10 | CefUnityLib 11 | CefUnityLib 12 | v4.6.1 13 | 512 14 | 15 | 16 | x64 17 | bin\x64\Debug\ 18 | 19 | 20 | x64 21 | bin\x64\Release\ 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /CefUnityLib/Consts.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CefUnityLib 8 | { 9 | public static class Consts 10 | { 11 | public const string NAMED_PIPE_PREFIX = "CUS_"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CefUnityLib/Helpers/Keys.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CefUnityLib.Helpers 4 | { 5 | // 6 | // Summary: 7 | // Specifies key codes and modifiers. 8 | [Flags] 9 | public enum Keys 10 | { 11 | // 12 | // Summary: 13 | // The bitmask to extract modifiers from a key value. 14 | Modifiers = -65536, 15 | // 16 | // Summary: 17 | // No key pressed. 18 | None = 0, 19 | // 20 | // Summary: 21 | // The left mouse button. 22 | LButton = 1, 23 | // 24 | // Summary: 25 | // The right mouse button. 26 | RButton = 2, 27 | // 28 | // Summary: 29 | // The CANCEL key. 30 | Cancel = 3, 31 | // 32 | // Summary: 33 | // The middle mouse button (three-button mouse). 34 | MButton = 4, 35 | // 36 | // Summary: 37 | // The first x mouse button (five-button mouse). 38 | XButton1 = 5, 39 | // 40 | // Summary: 41 | // The second x mouse button (five-button mouse). 42 | XButton2 = 6, 43 | // 44 | // Summary: 45 | // The BACKSPACE key. 46 | Back = 8, 47 | // 48 | // Summary: 49 | // The TAB key. 50 | Tab = 9, 51 | // 52 | // Summary: 53 | // The LINEFEED key. 54 | LineFeed = 10, 55 | // 56 | // Summary: 57 | // The CLEAR key. 58 | Clear = 12, 59 | // 60 | // Summary: 61 | // The RETURN key. 62 | Return = 13, 63 | // 64 | // Summary: 65 | // The ENTER key. 66 | Enter = 13, 67 | // 68 | // Summary: 69 | // The SHIFT key. 70 | ShiftKey = 16, 71 | // 72 | // Summary: 73 | // The CTRL key. 74 | ControlKey = 17, 75 | // 76 | // Summary: 77 | // The ALT key. 78 | Menu = 18, 79 | // 80 | // Summary: 81 | // The PAUSE key. 82 | Pause = 19, 83 | // 84 | // Summary: 85 | // The CAPS LOCK key. 86 | Capital = 20, 87 | // 88 | // Summary: 89 | // The CAPS LOCK key. 90 | CapsLock = 20, 91 | // 92 | // Summary: 93 | // The IME Kana mode key. 94 | KanaMode = 21, 95 | // 96 | // Summary: 97 | // The IME Hanguel mode key. (maintained for compatibility; use HangulMode) 98 | HanguelMode = 21, 99 | // 100 | // Summary: 101 | // The IME Hangul mode key. 102 | HangulMode = 21, 103 | // 104 | // Summary: 105 | // The IME Junja mode key. 106 | JunjaMode = 23, 107 | // 108 | // Summary: 109 | // The IME final mode key. 110 | FinalMode = 24, 111 | // 112 | // Summary: 113 | // The IME Hanja mode key. 114 | HanjaMode = 25, 115 | // 116 | // Summary: 117 | // The IME Kanji mode key. 118 | KanjiMode = 25, 119 | // 120 | // Summary: 121 | // The ESC key. 122 | Escape = 27, 123 | // 124 | // Summary: 125 | // The IME convert key. 126 | IMEConvert = 28, 127 | // 128 | // Summary: 129 | // The IME nonconvert key. 130 | IMENonconvert = 29, 131 | // 132 | // Summary: 133 | // The IME accept key, replaces System.Windows.Forms.Keys.IMEAceept. 134 | IMEAccept = 30, 135 | // 136 | // Summary: 137 | // The IME accept key. Obsolete, use System.Windows.Forms.Keys.IMEAccept instead. 138 | IMEAceept = 30, 139 | // 140 | // Summary: 141 | // The IME mode change key. 142 | IMEModeChange = 31, 143 | // 144 | // Summary: 145 | // The SPACEBAR key. 146 | Space = 32, 147 | // 148 | // Summary: 149 | // The PAGE UP key. 150 | Prior = 33, 151 | // 152 | // Summary: 153 | // The PAGE UP key. 154 | PageUp = 33, 155 | // 156 | // Summary: 157 | // The PAGE DOWN key. 158 | Next = 34, 159 | // 160 | // Summary: 161 | // The PAGE DOWN key. 162 | PageDown = 34, 163 | // 164 | // Summary: 165 | // The END key. 166 | End = 35, 167 | // 168 | // Summary: 169 | // The HOME key. 170 | Home = 36, 171 | // 172 | // Summary: 173 | // The LEFT ARROW key. 174 | Left = 37, 175 | // 176 | // Summary: 177 | // The UP ARROW key. 178 | Up = 38, 179 | // 180 | // Summary: 181 | // The RIGHT ARROW key. 182 | Right = 39, 183 | // 184 | // Summary: 185 | // The DOWN ARROW key. 186 | Down = 40, 187 | // 188 | // Summary: 189 | // The SELECT key. 190 | Select = 41, 191 | // 192 | // Summary: 193 | // The PRINT key. 194 | Print = 42, 195 | // 196 | // Summary: 197 | // The EXECUTE key. 198 | Execute = 43, 199 | // 200 | // Summary: 201 | // The PRINT SCREEN key. 202 | Snapshot = 44, 203 | // 204 | // Summary: 205 | // The PRINT SCREEN key. 206 | PrintScreen = 44, 207 | // 208 | // Summary: 209 | // The INS key. 210 | Insert = 45, 211 | // 212 | // Summary: 213 | // The DEL key. 214 | Delete = 46, 215 | // 216 | // Summary: 217 | // The HELP key. 218 | Help = 47, 219 | // 220 | // Summary: 221 | // The 0 key. 222 | D0 = 48, 223 | // 224 | // Summary: 225 | // The 1 key. 226 | D1 = 49, 227 | // 228 | // Summary: 229 | // The 2 key. 230 | D2 = 50, 231 | // 232 | // Summary: 233 | // The 3 key. 234 | D3 = 51, 235 | // 236 | // Summary: 237 | // The 4 key. 238 | D4 = 52, 239 | // 240 | // Summary: 241 | // The 5 key. 242 | D5 = 53, 243 | // 244 | // Summary: 245 | // The 6 key. 246 | D6 = 54, 247 | // 248 | // Summary: 249 | // The 7 key. 250 | D7 = 55, 251 | // 252 | // Summary: 253 | // The 8 key. 254 | D8 = 56, 255 | // 256 | // Summary: 257 | // The 9 key. 258 | D9 = 57, 259 | // 260 | // Summary: 261 | // The A key. 262 | A = 65, 263 | // 264 | // Summary: 265 | // The B key. 266 | B = 66, 267 | // 268 | // Summary: 269 | // The C key. 270 | C = 67, 271 | // 272 | // Summary: 273 | // The D key. 274 | D = 68, 275 | // 276 | // Summary: 277 | // The E key. 278 | E = 69, 279 | // 280 | // Summary: 281 | // The F key. 282 | F = 70, 283 | // 284 | // Summary: 285 | // The G key. 286 | G = 71, 287 | // 288 | // Summary: 289 | // The H key. 290 | H = 72, 291 | // 292 | // Summary: 293 | // The I key. 294 | I = 73, 295 | // 296 | // Summary: 297 | // The J key. 298 | J = 74, 299 | // 300 | // Summary: 301 | // The K key. 302 | K = 75, 303 | // 304 | // Summary: 305 | // The L key. 306 | L = 76, 307 | // 308 | // Summary: 309 | // The M key. 310 | M = 77, 311 | // 312 | // Summary: 313 | // The N key. 314 | N = 78, 315 | // 316 | // Summary: 317 | // The O key. 318 | O = 79, 319 | // 320 | // Summary: 321 | // The P key. 322 | P = 80, 323 | // 324 | // Summary: 325 | // The Q key. 326 | Q = 81, 327 | // 328 | // Summary: 329 | // The R key. 330 | R = 82, 331 | // 332 | // Summary: 333 | // The S key. 334 | S = 83, 335 | // 336 | // Summary: 337 | // The T key. 338 | T = 84, 339 | // 340 | // Summary: 341 | // The U key. 342 | U = 85, 343 | // 344 | // Summary: 345 | // The V key. 346 | V = 86, 347 | // 348 | // Summary: 349 | // The W key. 350 | W = 87, 351 | // 352 | // Summary: 353 | // The X key. 354 | X = 88, 355 | // 356 | // Summary: 357 | // The Y key. 358 | Y = 89, 359 | // 360 | // Summary: 361 | // The Z key. 362 | Z = 90, 363 | // 364 | // Summary: 365 | // The left Windows logo key (Microsoft Natural Keyboard). 366 | LWin = 91, 367 | // 368 | // Summary: 369 | // The right Windows logo key (Microsoft Natural Keyboard). 370 | RWin = 92, 371 | // 372 | // Summary: 373 | // The application key (Microsoft Natural Keyboard). 374 | Apps = 93, 375 | // 376 | // Summary: 377 | // The computer sleep key. 378 | Sleep = 95, 379 | // 380 | // Summary: 381 | // The 0 key on the numeric keypad. 382 | NumPad0 = 96, 383 | // 384 | // Summary: 385 | // The 1 key on the numeric keypad. 386 | NumPad1 = 97, 387 | // 388 | // Summary: 389 | // The 2 key on the numeric keypad. 390 | NumPad2 = 98, 391 | // 392 | // Summary: 393 | // The 3 key on the numeric keypad. 394 | NumPad3 = 99, 395 | // 396 | // Summary: 397 | // The 4 key on the numeric keypad. 398 | NumPad4 = 100, 399 | // 400 | // Summary: 401 | // The 5 key on the numeric keypad. 402 | NumPad5 = 101, 403 | // 404 | // Summary: 405 | // The 6 key on the numeric keypad. 406 | NumPad6 = 102, 407 | // 408 | // Summary: 409 | // The 7 key on the numeric keypad. 410 | NumPad7 = 103, 411 | // 412 | // Summary: 413 | // The 8 key on the numeric keypad. 414 | NumPad8 = 104, 415 | // 416 | // Summary: 417 | // The 9 key on the numeric keypad. 418 | NumPad9 = 105, 419 | // 420 | // Summary: 421 | // The multiply key. 422 | Multiply = 106, 423 | // 424 | // Summary: 425 | // The add key. 426 | Add = 107, 427 | // 428 | // Summary: 429 | // The separator key. 430 | Separator = 108, 431 | // 432 | // Summary: 433 | // The subtract key. 434 | Subtract = 109, 435 | // 436 | // Summary: 437 | // The decimal key. 438 | Decimal = 110, 439 | // 440 | // Summary: 441 | // The divide key. 442 | Divide = 111, 443 | // 444 | // Summary: 445 | // The F1 key. 446 | F1 = 112, 447 | // 448 | // Summary: 449 | // The F2 key. 450 | F2 = 113, 451 | // 452 | // Summary: 453 | // The F3 key. 454 | F3 = 114, 455 | // 456 | // Summary: 457 | // The F4 key. 458 | F4 = 115, 459 | // 460 | // Summary: 461 | // The F5 key. 462 | F5 = 116, 463 | // 464 | // Summary: 465 | // The F6 key. 466 | F6 = 117, 467 | // 468 | // Summary: 469 | // The F7 key. 470 | F7 = 118, 471 | // 472 | // Summary: 473 | // The F8 key. 474 | F8 = 119, 475 | // 476 | // Summary: 477 | // The F9 key. 478 | F9 = 120, 479 | // 480 | // Summary: 481 | // The F10 key. 482 | F10 = 121, 483 | // 484 | // Summary: 485 | // The F11 key. 486 | F11 = 122, 487 | // 488 | // Summary: 489 | // The F12 key. 490 | F12 = 123, 491 | // 492 | // Summary: 493 | // The F13 key. 494 | F13 = 124, 495 | // 496 | // Summary: 497 | // The F14 key. 498 | F14 = 125, 499 | // 500 | // Summary: 501 | // The F15 key. 502 | F15 = 126, 503 | // 504 | // Summary: 505 | // The F16 key. 506 | F16 = 127, 507 | // 508 | // Summary: 509 | // The F17 key. 510 | F17 = 128, 511 | // 512 | // Summary: 513 | // The F18 key. 514 | F18 = 129, 515 | // 516 | // Summary: 517 | // The F19 key. 518 | F19 = 130, 519 | // 520 | // Summary: 521 | // The F20 key. 522 | F20 = 131, 523 | // 524 | // Summary: 525 | // The F21 key. 526 | F21 = 132, 527 | // 528 | // Summary: 529 | // The F22 key. 530 | F22 = 133, 531 | // 532 | // Summary: 533 | // The F23 key. 534 | F23 = 134, 535 | // 536 | // Summary: 537 | // The F24 key. 538 | F24 = 135, 539 | // 540 | // Summary: 541 | // The NUM LOCK key. 542 | NumLock = 144, 543 | // 544 | // Summary: 545 | // The SCROLL LOCK key. 546 | Scroll = 145, 547 | // 548 | // Summary: 549 | // The left SHIFT key. 550 | LShiftKey = 160, 551 | // 552 | // Summary: 553 | // The right SHIFT key. 554 | RShiftKey = 161, 555 | // 556 | // Summary: 557 | // The left CTRL key. 558 | LControlKey = 162, 559 | // 560 | // Summary: 561 | // The right CTRL key. 562 | RControlKey = 163, 563 | // 564 | // Summary: 565 | // The left ALT key. 566 | LMenu = 164, 567 | // 568 | // Summary: 569 | // The right ALT key. 570 | RMenu = 165, 571 | // 572 | // Summary: 573 | // The browser back key (Windows 2000 or later). 574 | BrowserBack = 166, 575 | // 576 | // Summary: 577 | // The browser forward key (Windows 2000 or later). 578 | BrowserForward = 167, 579 | // 580 | // Summary: 581 | // The browser refresh key (Windows 2000 or later). 582 | BrowserRefresh = 168, 583 | // 584 | // Summary: 585 | // The browser stop key (Windows 2000 or later). 586 | BrowserStop = 169, 587 | // 588 | // Summary: 589 | // The browser search key (Windows 2000 or later). 590 | BrowserSearch = 170, 591 | // 592 | // Summary: 593 | // The browser favorites key (Windows 2000 or later). 594 | BrowserFavorites = 171, 595 | // 596 | // Summary: 597 | // The browser home key (Windows 2000 or later). 598 | BrowserHome = 172, 599 | // 600 | // Summary: 601 | // The volume mute key (Windows 2000 or later). 602 | VolumeMute = 173, 603 | // 604 | // Summary: 605 | // The volume down key (Windows 2000 or later). 606 | VolumeDown = 174, 607 | // 608 | // Summary: 609 | // The volume up key (Windows 2000 or later). 610 | VolumeUp = 175, 611 | // 612 | // Summary: 613 | // The media next track key (Windows 2000 or later). 614 | MediaNextTrack = 176, 615 | // 616 | // Summary: 617 | // The media previous track key (Windows 2000 or later). 618 | MediaPreviousTrack = 177, 619 | // 620 | // Summary: 621 | // The media Stop key (Windows 2000 or later). 622 | MediaStop = 178, 623 | // 624 | // Summary: 625 | // The media play pause key (Windows 2000 or later). 626 | MediaPlayPause = 179, 627 | // 628 | // Summary: 629 | // The launch mail key (Windows 2000 or later). 630 | LaunchMail = 180, 631 | // 632 | // Summary: 633 | // The select media key (Windows 2000 or later). 634 | SelectMedia = 181, 635 | // 636 | // Summary: 637 | // The start application one key (Windows 2000 or later). 638 | LaunchApplication1 = 182, 639 | // 640 | // Summary: 641 | // The start application two key (Windows 2000 or later). 642 | LaunchApplication2 = 183, 643 | // 644 | // Summary: 645 | // The OEM Semicolon key on a US standard keyboard (Windows 2000 or later). 646 | OemSemicolon = 186, 647 | // 648 | // Summary: 649 | // The OEM 1 key. 650 | Oem1 = 186, 651 | // 652 | // Summary: 653 | // The OEM plus key on any country/region keyboard (Windows 2000 or later). 654 | Oemplus = 187, 655 | // 656 | // Summary: 657 | // The OEM comma key on any country/region keyboard (Windows 2000 or later). 658 | Oemcomma = 188, 659 | // 660 | // Summary: 661 | // The OEM minus key on any country/region keyboard (Windows 2000 or later). 662 | OemMinus = 189, 663 | // 664 | // Summary: 665 | // The OEM period key on any country/region keyboard (Windows 2000 or later). 666 | OemPeriod = 190, 667 | // 668 | // Summary: 669 | // The OEM question mark key on a US standard keyboard (Windows 2000 or later). 670 | OemQuestion = 191, 671 | // 672 | // Summary: 673 | // The OEM 2 key. 674 | Oem2 = 191, 675 | // 676 | // Summary: 677 | // The OEM tilde key on a US standard keyboard (Windows 2000 or later). 678 | Oemtilde = 192, 679 | // 680 | // Summary: 681 | // The OEM 3 key. 682 | Oem3 = 192, 683 | // 684 | // Summary: 685 | // The OEM open bracket key on a US standard keyboard (Windows 2000 or later). 686 | OemOpenBrackets = 219, 687 | // 688 | // Summary: 689 | // The OEM 4 key. 690 | Oem4 = 219, 691 | // 692 | // Summary: 693 | // The OEM pipe key on a US standard keyboard (Windows 2000 or later). 694 | OemPipe = 220, 695 | // 696 | // Summary: 697 | // The OEM 5 key. 698 | Oem5 = 220, 699 | // 700 | // Summary: 701 | // The OEM close bracket key on a US standard keyboard (Windows 2000 or later). 702 | OemCloseBrackets = 221, 703 | // 704 | // Summary: 705 | // The OEM 6 key. 706 | Oem6 = 221, 707 | // 708 | // Summary: 709 | // The OEM singled/double quote key on a US standard keyboard (Windows 2000 or later). 710 | OemQuotes = 222, 711 | // 712 | // Summary: 713 | // The OEM 7 key. 714 | Oem7 = 222, 715 | // 716 | // Summary: 717 | // The OEM 8 key. 718 | Oem8 = 223, 719 | // 720 | // Summary: 721 | // The OEM angle bracket or backslash key on the RT 102 key keyboard (Windows 2000 722 | // or later). 723 | OemBackslash = 226, 724 | // 725 | // Summary: 726 | // The OEM 102 key. 727 | Oem102 = 226, 728 | // 729 | // Summary: 730 | // The PROCESS KEY key. 731 | ProcessKey = 229, 732 | // 733 | // Summary: 734 | // Used to pass Unicode characters as if they were keystrokes. The Packet key value 735 | // is the low word of a 32-bit virtual-key value used for non-keyboard input methods. 736 | Packet = 231, 737 | // 738 | // Summary: 739 | // The ATTN key. 740 | Attn = 246, 741 | // 742 | // Summary: 743 | // The CRSEL key. 744 | Crsel = 247, 745 | // 746 | // Summary: 747 | // The EXSEL key. 748 | Exsel = 248, 749 | // 750 | // Summary: 751 | // The ERASE EOF key. 752 | EraseEof = 249, 753 | // 754 | // Summary: 755 | // The PLAY key. 756 | Play = 250, 757 | // 758 | // Summary: 759 | // The ZOOM key. 760 | Zoom = 251, 761 | // 762 | // Summary: 763 | // A constant reserved for future use. 764 | NoName = 252, 765 | // 766 | // Summary: 767 | // The PA1 key. 768 | Pa1 = 253, 769 | // 770 | // Summary: 771 | // The CLEAR key. 772 | OemClear = 254, 773 | // 774 | // Summary: 775 | // The bitmask to extract a key code from a key value. 776 | KeyCode = 65535, 777 | // 778 | // Summary: 779 | // The SHIFT modifier key. 780 | Shift = 65536, 781 | // 782 | // Summary: 783 | // The CTRL modifier key. 784 | Control = 131072, 785 | // 786 | // Summary: 787 | // The ALT modifier key. 788 | Alt = 262144 789 | } 790 | } -------------------------------------------------------------------------------- /CefUnityLib/Helpers/MouseButtons.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CefUnityLib.Helpers 4 | { 5 | // 6 | // Summary: 7 | // Specifies constants that define which mouse button was pressed. 8 | [Flags] 9 | public enum MouseButtons 10 | { 11 | // 12 | // Summary: 13 | // No mouse button was pressed. 14 | None = 0, 15 | // 16 | // Summary: 17 | // The left mouse button was pressed. 18 | Left = 1048576, 19 | // 20 | // Summary: 21 | // The right mouse button was pressed. 22 | Right = 2097152, 23 | // 24 | // Summary: 25 | // The middle mouse button was pressed. 26 | Middle = 4194304, 27 | // 28 | // Summary: 29 | // The first XButton was pressed. 30 | XButton1 = 8388608, 31 | // 32 | // Summary: 33 | // The second XButton was pressed. 34 | XButton2 = 16777216 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /CefUnityLib/Messages/KeyEventPipeMessage.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib.Helpers; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CefUnityLib.Messages 9 | { 10 | public class KeyEventPipeMessage : PipeProtoMessage 11 | { 12 | public const byte TYPE_KEY_DOWN = 0; 13 | public const byte TYPE_KEY_UP = 1; 14 | public const byte TYPE_KEY_CHAR = 2; 15 | 16 | public const byte IDX_EVENT_TYPE = 0; 17 | public const byte IDX_KEY_CODE = 1; 18 | public const byte IDX_MODIFIERS = 2; 19 | 20 | public KeyEventPipeMessage(byte[] payload) 21 | { 22 | Opcode = PipeProto.OPCODE_KEY_EVENT; 23 | Payload = payload; 24 | } 25 | 26 | public KeyEventPipeMessage(byte eventType, Keys keys, Keys modifiers = Helpers.Keys.None) 27 | { 28 | Opcode = PipeProto.OPCODE_KEY_EVENT; 29 | 30 | Payload = new byte[1 + 4 + 4 + 4]; // [Opcode/1b] + [Int32/KeyCode/4] + [Int32/Modifiers/4] 31 | 32 | KeyEventType = eventType; 33 | Keys = keys; 34 | Modifiers = modifiers; 35 | } 36 | 37 | public KeyEventPipeMessage(byte eventType, int keyCode, Keys modifiers = Helpers.Keys.None) 38 | : this(eventType, Keys.None, modifiers) 39 | { 40 | KeysInt = keyCode; 41 | } 42 | 43 | public byte KeyEventType 44 | { 45 | get 46 | { 47 | return Payload[0]; 48 | } 49 | 50 | set 51 | { 52 | Payload[IDX_EVENT_TYPE] = value; 53 | } 54 | } 55 | 56 | public Keys Keys 57 | { 58 | get 59 | { 60 | return (Keys)KeysInt; 61 | } 62 | 63 | set 64 | { 65 | KeysInt = (int)value; 66 | } 67 | } 68 | 69 | public int KeysInt 70 | { 71 | get 72 | { 73 | return BitConverter.ToInt32(Payload.Skip(IDX_KEY_CODE).Take(4).ToArray(), 0); 74 | } 75 | 76 | set 77 | { 78 | BitConverter.GetBytes(value).CopyTo(Payload, IDX_KEY_CODE); 79 | } 80 | } 81 | 82 | public Keys Modifiers 83 | { 84 | get 85 | { 86 | return (Keys)BitConverter.ToInt32(Payload.Skip(IDX_MODIFIERS).Take(4).ToArray(), 0); 87 | } 88 | 89 | set 90 | { 91 | BitConverter.GetBytes((int)value).CopyTo(Payload, IDX_MODIFIERS); 92 | } 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /CefUnityLib/Messages/MouseEventPipeMessage.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib.Helpers; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CefUnityLib.Messages 9 | { 10 | public class MouseEventPipeMessage : PipeProtoMessage 11 | { 12 | public const byte TYPE_MOVE = 0; 13 | public const byte TYPE_MOUSE_DOWN = 1; 14 | public const byte TYPE_MOUSE_UP = 2; 15 | 16 | public const byte IDX_EVENT_TYPE = 0; 17 | public const byte IDX_COORD_X = 1; 18 | public const byte IDX_COORD_Y = 5; 19 | public const byte IDX_BUTTON = 9; 20 | 21 | public const byte BUTTON_LEFT = 0; 22 | public const byte BUTTON_MID = 1; 23 | public const byte BUTTON_RIGHT = 2; 24 | 25 | public MouseEventPipeMessage(byte[] payload) 26 | { 27 | Opcode = PipeProto.OPCODE_MOUSE_EVENT; 28 | Payload = payload; 29 | } 30 | 31 | public MouseEventPipeMessage(byte eventType = TYPE_MOVE, Int32 coordX = 0, Int32 coordY = 0, MouseButtons mouseButtons = MouseButtons.Left) 32 | { 33 | Opcode = PipeProto.OPCODE_MOUSE_EVENT; 34 | 35 | Payload = new byte[1 + 4 + 4 + 4]; // [Opcode/1b] + [Int32/CoordX/4] + [Int32/CoordY/4] + [Int32/Button/4] 36 | 37 | MouseEventType = eventType; 38 | CoordX = coordX; 39 | CoordY = coordY; 40 | MouseButtons = mouseButtons; 41 | } 42 | 43 | public byte MouseEventType 44 | { 45 | get 46 | { 47 | return Payload[IDX_EVENT_TYPE]; 48 | } 49 | 50 | set 51 | { 52 | Payload[IDX_EVENT_TYPE] = value; 53 | } 54 | } 55 | 56 | public Int32 CoordX 57 | { 58 | get 59 | { 60 | return BitConverter.ToInt32(Payload.Skip(IDX_COORD_X).Take(4).ToArray(), 0); 61 | } 62 | 63 | set 64 | { 65 | BitConverter.GetBytes(value).CopyTo(Payload, IDX_COORD_X); 66 | } 67 | } 68 | 69 | public Int32 CoordY 70 | { 71 | get 72 | { 73 | return BitConverter.ToInt32(Payload.Skip(IDX_COORD_Y).Take(4).ToArray(), 0); 74 | } 75 | 76 | set 77 | { 78 | BitConverter.GetBytes(value).CopyTo(Payload, IDX_COORD_Y); 79 | } 80 | } 81 | 82 | public MouseButtons MouseButtons 83 | { 84 | get 85 | { 86 | return (MouseButtons)(BitConverter.ToInt32(Payload.Skip(IDX_BUTTON).Take(4).ToArray(), 0)); 87 | } 88 | 89 | set 90 | { 91 | BitConverter.GetBytes((int)value).CopyTo(Payload, IDX_BUTTON); 92 | } 93 | } 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /CefUnityLib/Messages/MouseWheelEventPipeMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace CefUnityLib.Messages 5 | { 6 | public class MouseWheelEventPipeMessage : PipeProtoMessage 7 | { 8 | public const byte IDX_COORD_X = 0; 9 | public const byte IDX_COORD_Y = 4; 10 | public const byte IDX_DELTA = 8; 11 | 12 | public MouseWheelEventPipeMessage(byte[] payload) 13 | { 14 | Opcode = PipeProto.OPCODE_MOUSE_WHEEL_EVENT; 15 | Payload = payload; 16 | } 17 | 18 | public MouseWheelEventPipeMessage(int x, int y, int delta) 19 | { 20 | Opcode = PipeProto.OPCODE_MOUSE_WHEEL_EVENT; 21 | 22 | Payload = new byte[4 + 4 + 4]; // [Int32/CoordX/4] + [Int32/CoordY/4] + [Int32/Delta/4] 23 | 24 | CoordX = x; 25 | CoordY = y; 26 | Delta = delta; 27 | } 28 | 29 | public Int32 CoordX 30 | { 31 | get 32 | { 33 | return BitConverter.ToInt32(Payload.Skip(IDX_COORD_X).Take(4).ToArray(), 0); 34 | } 35 | 36 | set 37 | { 38 | BitConverter.GetBytes(value).CopyTo(Payload, IDX_COORD_X); 39 | } 40 | } 41 | 42 | public Int32 CoordY 43 | { 44 | get 45 | { 46 | return BitConverter.ToInt32(Payload.Skip(IDX_COORD_Y).Take(4).ToArray(), 0); 47 | } 48 | 49 | set 50 | { 51 | BitConverter.GetBytes(value).CopyTo(Payload, IDX_COORD_Y); 52 | } 53 | } 54 | 55 | public Int32 Delta 56 | { 57 | get 58 | { 59 | return BitConverter.ToInt32(Payload.Skip(IDX_DELTA).Take(4).ToArray(), 0); 60 | } 61 | 62 | set 63 | { 64 | BitConverter.GetBytes(value).CopyTo(Payload, IDX_DELTA); 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /CefUnityLib/PipeProto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CefUnityLib 8 | { 9 | public static class PipeProto 10 | { 11 | public const byte PACKET_START_MARKER = 2; 12 | 13 | public const byte LENGTH_MARKER_SIZE = 4; 14 | 15 | public const byte OPCODE_FRAME = 1; 16 | public const byte OPCODE_RESIZE = 2; 17 | public const byte OPCODE_NAVIGATE = 3; 18 | public const byte OPCODE_SCRIPT = 4; 19 | public const byte OPCODE_PING = 5; 20 | public const byte OPCODE_MOUSE_EVENT = 6; 21 | public const byte OPCODE_KEY_EVENT = 7; 22 | public const byte OPCODE_SHUTDOWN = 8; 23 | public const byte OPCODE_MOUSE_WHEEL_EVENT = 9; 24 | 25 | public static byte[] BytesToProtoMessage(byte[] input, byte opcode) 26 | { 27 | if (input == null) 28 | { 29 | input = new byte[0]; 30 | } 31 | 32 | // [START BYTE] + [OPCODE] + [LENGTH MARKER] + [PAYLOAD] 33 | byte[] output = new byte[1 + 1 + LENGTH_MARKER_SIZE + input.Length]; 34 | var idx = 0; 35 | 36 | // Start marker and opcode 37 | output[idx++] = PACKET_START_MARKER; 38 | output[idx++] = opcode; 39 | 40 | // Length marker 41 | var lengthMarkerBytes = BitConverter.GetBytes((UInt32)input.Length); 42 | lengthMarkerBytes.CopyTo(output, idx); 43 | 44 | idx += LENGTH_MARKER_SIZE; 45 | 46 | // Payload 47 | input.CopyTo(output, idx); 48 | 49 | // All done 50 | return output; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /CefUnityLib/PipeProtoMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.IO; 5 | using System.IO.Pipes; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace CefUnityLib 11 | { 12 | public class PipeProtoMessage 13 | { 14 | public byte Opcode; 15 | public byte[] Payload; 16 | 17 | public byte[] ToBytes() 18 | { 19 | return PipeProto.BytesToProtoMessage(Payload, Opcode); 20 | } 21 | 22 | public void WriteToClientStream(NamedPipeClientStream stream, bool flush) 23 | { 24 | var bytes = ToBytes(); 25 | 26 | lock (stream) 27 | { 28 | stream.Write(bytes, 0, bytes.Length); 29 | 30 | if (flush) 31 | { 32 | stream.Flush(); 33 | } 34 | } 35 | } 36 | 37 | public static PipeProtoMessage ReadFromStream(Stream stream) 38 | { 39 | int headerLengthExpected = 1 + 1 + 4; 40 | 41 | byte[] nextHeader = new byte[headerLengthExpected]; 42 | int headerBytesRead = stream.Read(nextHeader, 0, nextHeader.Length); 43 | 44 | if (headerBytesRead == headerLengthExpected) 45 | { 46 | byte index = 0; 47 | byte firstByte = nextHeader[index++]; 48 | 49 | if (firstByte == PipeProto.PACKET_START_MARKER) 50 | { 51 | byte packetOpcode = nextHeader[index++]; 52 | 53 | UInt32 payloadLength = BitConverter.ToUInt32(new byte[] { nextHeader[index++], nextHeader[index++], 54 | nextHeader[index++], nextHeader[index++] }, 0); 55 | 56 | var packetParsed = new PipeProtoMessage 57 | { 58 | Opcode = packetOpcode, 59 | Payload = null 60 | }; 61 | 62 | if (payloadLength > 0) 63 | { 64 | packetParsed.Payload = new byte[payloadLength]; 65 | stream.Read(packetParsed.Payload, 0, (int)payloadLength); 66 | } 67 | 68 | return packetParsed; 69 | } 70 | } 71 | 72 | if (headerBytesRead == 0) 73 | { 74 | throw new InvalidOperationException("Connection is now closed, cannot read data."); 75 | } 76 | 77 | return null; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /CefUnityLib/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CefUnityLib")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CefUnityLib")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("483e28cb-9845-4ab7-ba58-13a579a155f7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /CefUnityServer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27428.2027 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CefUnityServer", "CefUnityServer\CefUnityServer.csproj", "{593A3A77-849E-49CD-808E-EDC356986337}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CefUnityLib", "CefUnityLib\CefUnityLib.csproj", "{483E28CB-9845-4AB7-BA58-13A579A155F7}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CefUnityTestClient", "CefUnityTestClient\CefUnityTestClient.csproj", "{4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SampleUnityIntegration", "SampleUnityIntegration\SampleUnityIntegration.csproj", "{2408CB96-2502-410C-99C4-61F45455B50F}" 13 | EndProject 14 | Global 15 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 16 | Debug|Any CPU = Debug|Any CPU 17 | Debug|x64 = Debug|x64 18 | Release|Any CPU = Release|Any CPU 19 | Release|x64 = Release|x64 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {593A3A77-849E-49CD-808E-EDC356986337}.Debug|Any CPU.ActiveCfg = Debug|x64 23 | {593A3A77-849E-49CD-808E-EDC356986337}.Debug|x64.ActiveCfg = Debug|x64 24 | {593A3A77-849E-49CD-808E-EDC356986337}.Debug|x64.Build.0 = Debug|x64 25 | {593A3A77-849E-49CD-808E-EDC356986337}.Release|Any CPU.ActiveCfg = Release|x64 26 | {593A3A77-849E-49CD-808E-EDC356986337}.Release|x64.ActiveCfg = Release|x64 27 | {593A3A77-849E-49CD-808E-EDC356986337}.Release|x64.Build.0 = Release|x64 28 | {593A3A77-849E-49CD-808E-EDC356986337}.Debug|Any CPU.Build.0 = Debug|x64 29 | {483E28CB-9845-4AB7-BA58-13A579A155F7}.Debug|Any CPU.ActiveCfg = Debug|x64 30 | {483E28CB-9845-4AB7-BA58-13A579A155F7}.Debug|x64.ActiveCfg = Debug|x64 31 | {483E28CB-9845-4AB7-BA58-13A579A155F7}.Debug|x64.Build.0 = Debug|x64 32 | {483E28CB-9845-4AB7-BA58-13A579A155F7}.Release|Any CPU.ActiveCfg = Release|x64 33 | {483E28CB-9845-4AB7-BA58-13A579A155F7}.Release|x64.ActiveCfg = Release|x64 34 | {483E28CB-9845-4AB7-BA58-13A579A155F7}.Release|x64.Build.0 = Release|x64 35 | {483E28CB-9845-4AB7-BA58-13A579A155F7}.Debug|Any CPU.Build.0 = Debug|x64 36 | {4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109}.Debug|Any CPU.ActiveCfg = Debug|x64 37 | {4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109}.Debug|x64.ActiveCfg = Debug|x64 38 | {4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109}.Debug|x64.Build.0 = Debug|x64 39 | {4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109}.Release|Any CPU.ActiveCfg = Release|x64 40 | {4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109}.Release|x64.ActiveCfg = Release|x64 41 | {4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109}.Release|x64.Build.0 = Release|x64 42 | {4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109}.Debug|Any CPU.Build.0 = Debug|x64 43 | {2408CB96-2502-410C-99C4-61F45455B50F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 44 | {2408CB96-2502-410C-99C4-61F45455B50F}.Debug|x64.ActiveCfg = Debug|Any CPU 45 | {2408CB96-2502-410C-99C4-61F45455B50F}.Debug|x64.Build.0 = Debug|Any CPU 46 | {2408CB96-2502-410C-99C4-61F45455B50F}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {2408CB96-2502-410C-99C4-61F45455B50F}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {2408CB96-2502-410C-99C4-61F45455B50F}.Release|x64.ActiveCfg = Release|Any CPU 49 | {2408CB96-2502-410C-99C4-61F45455B50F}.Release|x64.Build.0 = Release|Any CPU 50 | EndGlobalSection 51 | GlobalSection(SolutionProperties) = preSolution 52 | HideSolutionNode = FALSE 53 | EndGlobalSection 54 | GlobalSection(ExtensibilityGlobals) = postSolution 55 | SolutionGuid = {CA2537D0-43BC-4A9C-B90D-ED65A77E0817} 56 | EndGlobalSection 57 | EndGlobal 58 | -------------------------------------------------------------------------------- /CefUnityServer/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CefUnityServer/BrowserHost.cs: -------------------------------------------------------------------------------- 1 | using CefSharp; 2 | using CefSharp.OffScreen; 3 | using CefUnityLib.Helpers; 4 | using CefUnityLib.Messages; 5 | using CefUnityServer.Tasks; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Runtime.ExceptionServices; 10 | using System.Runtime.InteropServices; 11 | using System.Text; 12 | using System.Threading; 13 | using System.Threading.Tasks; 14 | 15 | namespace CefUnityServer 16 | { 17 | public class BrowserHost : IContextMenuHandler, ILifeSpanHandler 18 | { 19 | protected const int BROWSER_INIT_TIMEOUT_MS = 15000; 20 | 21 | protected TaskRunner runner; 22 | protected BrowserSettings settings; 23 | 24 | protected RequestContext requestContext; 25 | protected ChromiumWebBrowser webBrowser; 26 | 27 | protected byte[] paintBitmap = null; 28 | protected int paintBufferSize = 0; 29 | 30 | public BrowserHost(TaskRunner runner) : this(runner, new BrowserSettings()) 31 | { 32 | } 33 | 34 | public BrowserHost(TaskRunner runner, BrowserSettings settings) 35 | { 36 | this.runner = runner; 37 | this.settings = settings; 38 | } 39 | 40 | public async void Start() 41 | { 42 | // Settings modifiers 43 | this.settings.WindowlessFrameRate = 60; 44 | this.settings.BackgroundColor = 0x00; 45 | 46 | // CEF init with custom settings 47 | var cefSettings = new CefSettings(); 48 | cefSettings.CefCommandLineArgs["disable-extensions"] = "1"; 49 | cefSettings.CefCommandLineArgs["disable-gpu"] = "1"; 50 | cefSettings.CefCommandLineArgs["disable-gpu-compositing"] = "1"; 51 | cefSettings.CefCommandLineArgs["enable-begin-frame-scheduling"] = "1"; 52 | cefSettings.CefCommandLineArgs["enable-experimental-web-platform-features"] = "1"; 53 | cefSettings.CefCommandLineArgs["enable-media-stream"] = "1"; 54 | cefSettings.CefCommandLineArgs["enable-precise-memory-info"] = "1"; 55 | cefSettings.CefCommandLineArgs["autoplay-policy"] = "no-user-gesture-required"; 56 | cefSettings.CefCommandLineArgs.Remove("mute-audio"); 57 | Cef.Initialize(cefSettings); 58 | 59 | // Request context 60 | var reqCtxSettings = new RequestContextSettings 61 | { 62 | CachePath = "", 63 | IgnoreCertificateErrors = false, 64 | PersistSessionCookies = false, 65 | PersistUserPreferences = false, 66 | }; 67 | 68 | this.requestContext = new RequestContext(reqCtxSettings); 69 | 70 | // Browser window 71 | this.webBrowser = new ChromiumWebBrowser("about:blank", this.settings, this.requestContext, false); 72 | this.webBrowser.MenuHandler = this; 73 | this.webBrowser.LifeSpanHandler = this; 74 | this.webBrowser.CreateBrowser(); 75 | 76 | // Resize and wait for init 77 | Resize(1024, 768); 78 | WaitForBrowserInit(); 79 | 80 | // Bind events 81 | this.webBrowser.Paint += WebBrowser_Paint; 82 | this.webBrowser.LoadError += WebBrowser_LoadError; 83 | this.webBrowser.LoadingStateChanged += WebBrowser_LoadingStateChanged; 84 | 85 | // Load initial page (TEST / TEMP) 86 | await LoadPageAsync("https://www.youtube.com/watch?v=KaOC9danxNo"); 87 | } 88 | 89 | public void Stop() 90 | { 91 | if (this.webBrowser != null) 92 | { 93 | this.webBrowser.GetBrowserHost().CloseBrowser(true); 94 | 95 | this.webBrowser.Dispose(); 96 | this.webBrowser = null; 97 | 98 | this.requestContext.Dispose(); 99 | this.requestContext = null; 100 | } 101 | 102 | Cef.Shutdown(); 103 | } 104 | 105 | protected bool LmbIsDown = false; 106 | 107 | public object CefMouseButtonType { get; private set; } 108 | 109 | public void HandleKeyEvent(KeyEventPipeMessage keyMessage) 110 | { 111 | var host = this.webBrowser.GetBrowserHost(); 112 | 113 | // Get character info 114 | int character = 0; 115 | 116 | var cefKeyModifiers = new CefEventFlags(); 117 | 118 | if (keyMessage.KeyEventType != KeyEventPipeMessage.TYPE_KEY_CHAR) 119 | { 120 | var keyFlags = keyMessage.Keys; 121 | var keyFlagsChar = keyFlags & Keys.KeyCode; // bit shift to remove modifiers from character code 122 | 123 | character = (int)keyFlagsChar; 124 | 125 | if (keyMessage.Modifiers.HasFlag(Keys.Control)) 126 | { 127 | cefKeyModifiers |= CefEventFlags.ControlDown; 128 | } 129 | 130 | if (keyMessage.Modifiers.HasFlag(Keys.Shift)) 131 | { 132 | cefKeyModifiers |= CefEventFlags.ShiftDown; 133 | } 134 | 135 | if (keyMessage.Modifiers.HasFlag(Keys.Alt)) 136 | { 137 | cefKeyModifiers |= CefEventFlags.AltDown; 138 | } 139 | 140 | //if (keyMessage.KeyEventType == 0) 141 | //{ 142 | // Logr.Log( 143 | // keyMessage.KeyEventType <= 0 ? "Key down:" : "Key up:", 144 | // keyMessage.Keys, 145 | // keyMessage.Modifiers != Keys.None ? "+ Mod " + keyMessage.Modifiers.ToString() : "" 146 | // ); 147 | //} 148 | } 149 | else 150 | { 151 | character = (int)keyMessage.Keys; 152 | //Logr.Log("Char down:", (char)character, character); 153 | } 154 | 155 | var keyEvent = new KeyEvent() 156 | { 157 | Type = KeyEventType.Char, 158 | WindowsKeyCode = character, 159 | Modifiers = cefKeyModifiers 160 | }; 161 | 162 | if (keyMessage.KeyEventType == KeyEventPipeMessage.TYPE_KEY_DOWN) 163 | keyEvent.Type = KeyEventType.KeyDown; 164 | 165 | if (keyMessage.KeyEventType == KeyEventPipeMessage.TYPE_KEY_UP) 166 | keyEvent.Type = KeyEventType.KeyUp; 167 | 168 | host.SendKeyEvent(keyEvent); 169 | } 170 | 171 | public void HandleMouseEvent(MouseEventPipeMessage mouseMessage) 172 | { 173 | var host = this.webBrowser.GetBrowserHost(); 174 | 175 | // Read X & Y coords 176 | int x = mouseMessage.CoordX; 177 | int y = mouseMessage.CoordY; 178 | 179 | // Read primary event button & generate modifier struct 180 | var modifiers = new CefEventFlags(); 181 | var mouseButton = MouseButtonType.Left; 182 | 183 | if (mouseMessage.MouseButtons == MouseButtons.Left) 184 | { 185 | modifiers |= CefEventFlags.LeftMouseButton; 186 | mouseButton = MouseButtonType.Left; 187 | } 188 | 189 | if (mouseMessage.MouseButtons == MouseButtons.Right) 190 | { 191 | modifiers |= CefEventFlags.RightMouseButton; 192 | mouseButton = MouseButtonType.Right; 193 | } 194 | 195 | if (mouseMessage.MouseButtons == MouseButtons.Middle) 196 | { 197 | modifiers |= CefEventFlags.MiddleMouseButton; 198 | mouseButton = MouseButtonType.Middle; 199 | } 200 | 201 | // Generate generic event 202 | var mouseEvent = new MouseEvent(x, y, modifiers); 203 | 204 | // Dispatch event to browser host 205 | if (mouseMessage.MouseEventType == MouseEventPipeMessage.TYPE_MOVE) 206 | { 207 | host.SendMouseMoveEvent(mouseEvent, false); 208 | } 209 | else 210 | { 211 | bool isUpEvent = (mouseMessage.MouseEventType == MouseEventPipeMessage.TYPE_MOUSE_UP); 212 | host.SendMouseClickEvent(mouseEvent, mouseButton, isUpEvent, 1); 213 | } 214 | } 215 | 216 | public void HandleMouseWheelEvent(MouseWheelEventPipeMessage mouseWheelMsg) 217 | { 218 | var host = this.webBrowser.GetBrowserHost(); 219 | 220 | int x = mouseWheelMsg.CoordX; 221 | int y = mouseWheelMsg.CoordY; 222 | int delta = mouseWheelMsg.Delta; 223 | 224 | var mouseEvent = new MouseEvent(x, y, CefEventFlags.None); 225 | host.SendMouseWheelEvent(mouseEvent, 0, delta); 226 | } 227 | 228 | private void WebBrowser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e) 229 | { 230 | // Ensure browser is in "focus" mode so that mouse move events are handled 231 | this.webBrowser.GetBrowserHost().SendFocusEvent(true); 232 | } 233 | 234 | private void WebBrowser_LoadError(object sender, LoadErrorEventArgs e) 235 | { 236 | // TODO 237 | } 238 | 239 | public void Resize(int width, int height) 240 | { 241 | // Resize 242 | this.webBrowser.Size = new System.Drawing.Size(width, height); 243 | 244 | // Reset frame buffer 245 | paintBitmap = null; 246 | 247 | // Force repaint if browser is ready 248 | Repaint(); 249 | 250 | Logr.Log(String.Format("Browser host: Resized viewport to {0}x{1}.", width, height)); 251 | } 252 | 253 | public void Repaint() 254 | { 255 | if (webBrowser.IsBrowserInitialized) 256 | { 257 | var host = this.webBrowser.GetBrowserHost(); 258 | 259 | if (host != null) 260 | { 261 | host.Invalidate(PaintElementType.View); 262 | } 263 | } 264 | } 265 | 266 | [HandleProcessCorruptedStateExceptions] 267 | private void WebBrowser_Paint(object sender, OnPaintEventArgs e) 268 | { 269 | // pixel data for the image: width * height * 4 bytes 270 | // BGRA image with upper-left origin 271 | 272 | int numberOfBytes = e.Height * e.Width * 4; 273 | 274 | if (paintBitmap == null || numberOfBytes != paintBitmap.Length) 275 | { 276 | paintBufferSize = numberOfBytes; 277 | paintBitmap = new byte[paintBufferSize]; 278 | 279 | Logr.Log("Paint buffer: Resizing to", paintBufferSize, "bytes"); 280 | } 281 | 282 | try 283 | { 284 | Marshal.Copy(e.BufferHandle, paintBitmap, 0, paintBufferSize); 285 | } 286 | catch (AccessViolationException) 287 | { 288 | Logr.Log("WARN: Marshal copy failed: Access violation while reading frame buffer."); 289 | } 290 | 291 | this.runner.AddTask(new SendFrameTask(paintBitmap)); 292 | } 293 | 294 | public Task LoadPageAsync(string address = null) 295 | { 296 | var tcs = new TaskCompletionSource(); 297 | EventHandler handler = null; 298 | 299 | handler = (sender, args) => 300 | { 301 | if (!args.IsLoading) 302 | { 303 | this.webBrowser.LoadingStateChanged -= handler; 304 | tcs.TrySetResult(true); 305 | } 306 | }; 307 | 308 | this.webBrowser.LoadingStateChanged += handler; 309 | 310 | if (!string.IsNullOrEmpty(address)) 311 | { 312 | this.webBrowser.Load(address); 313 | } 314 | 315 | return tcs.Task; 316 | } 317 | 318 | public void WaitForBrowserInit() 319 | { 320 | int remainingTries = BROWSER_INIT_TIMEOUT_MS; 321 | 322 | while (!this.webBrowser.IsBrowserInitialized) 323 | { 324 | if (remainingTries <= 0) 325 | { 326 | throw new TimeoutException(String.Format("Browser failed to initialize after a timeout of {0} ms.", BROWSER_INIT_TIMEOUT_MS)); 327 | } 328 | 329 | Thread.Sleep(100); 330 | remainingTries -= 100; 331 | } 332 | } 333 | 334 | public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model) 335 | { 336 | model.Clear(); 337 | } 338 | 339 | public bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags) 340 | { 341 | return false; 342 | } 343 | 344 | public void OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame) 345 | { 346 | // 347 | } 348 | 349 | public bool RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback) 350 | { 351 | return false; 352 | } 353 | 354 | public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) 355 | { 356 | newBrowser = null; 357 | return true; 358 | } 359 | 360 | public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser) 361 | { 362 | // 363 | } 364 | 365 | public bool DoClose(IWebBrowser browserControl, IBrowser browser) 366 | { 367 | return false; 368 | } 369 | 370 | public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser) 371 | { 372 | // 373 | } 374 | } 375 | } -------------------------------------------------------------------------------- /CefUnityServer/CefUnityServer.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | Debug 9 | AnyCPU 10 | {593A3A77-849E-49CD-808E-EDC356986337} 11 | Exe 12 | CefUnityServer 13 | CefUnityServer 14 | v4.6.1 15 | 512 16 | true 17 | 18 | 19 | 20 | 21 | x64 22 | bin\x64\Debug\ 23 | 24 | 25 | x64 26 | bin\x64\Release\ 27 | 28 | 29 | CefUnityServer.Program 30 | 31 | 32 | 33 | ..\packages\CefSharp.Common.90.6.50\lib\net452\CefSharp.dll 34 | True 35 | 36 | 37 | ..\packages\CefSharp.Common.90.6.50\lib\net452\CefSharp.Core.dll 38 | True 39 | 40 | 41 | ..\packages\CefSharp.OffScreen.90.6.50\lib\net452\CefSharp.OffScreen.dll 42 | True 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | {483e28cb-9845-4ab7-ba58-13a579a155f7} 76 | CefUnityLib 77 | 78 | 79 | 80 | 81 | 82 | This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}. 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /CefUnityServer/ITaskRunnable.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CefUnityServer 8 | { 9 | public interface ITaskRunnable 10 | { 11 | void Run(BrowserHost host, PipeServer server); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /CefUnityServer/Logr.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CefUnityServer 9 | { 10 | public static class Logr 11 | { 12 | private static Object syncLock = new object(); 13 | private static DateTime? firstLog; 14 | 15 | private static string MakeTimeStamp() 16 | { 17 | if (!firstLog.HasValue) 18 | { 19 | firstLog = DateTime.Now; 20 | } 21 | 22 | var secondsSinceStart = (DateTime.Now - firstLog).Value.TotalSeconds; 23 | var secondsFormatted = secondsSinceStart.ToString("N3").Replace(',', '.'); 24 | 25 | var padToLength = 10; 26 | 27 | var sb = new StringBuilder(); 28 | sb.Append("["); 29 | while (sb.Length < (padToLength - 2 - secondsFormatted.Length)) 30 | sb.Append(" "); 31 | sb.Append(secondsFormatted); 32 | sb.Append("]"); 33 | 34 | return sb.ToString(); 35 | } 36 | 37 | public static void Log(params object[] things) 38 | { 39 | lock (syncLock) 40 | { 41 | var jointMessage = 42 | ( 43 | MakeTimeStamp() + " " + 44 | String.Join(" ", things) 45 | ); 46 | 47 | if (Console.Out != null) 48 | { 49 | Console.WriteLine(jointMessage); 50 | } 51 | 52 | Debug.WriteLine(jointMessage); 53 | } 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /CefUnityServer/PipeServer.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib; 2 | using CefUnityServer.Tasks; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.IO.Pipes; 7 | using System.Text; 8 | using System.Threading; 9 | using System.Threading.Tasks; 10 | 11 | namespace CefUnityServer 12 | { 13 | public class PipeServer 14 | { 15 | public const int NO_COMM_AUTO_SHUTDOWN_AFTER_SECS = 60; 16 | 17 | protected string pipeName; 18 | protected NamedPipeServerStream stream; 19 | protected bool KeepAlive = true; 20 | protected object syncLock = new object(); 21 | protected TaskRunner runner; 22 | protected bool GotRemotePing = false; 23 | protected DateTime LastActivity; 24 | 25 | public PipeServer(string pipeName, TaskRunner runner) 26 | { 27 | // Auto prefix pipe name 28 | if (!pipeName.StartsWith(Consts.NAMED_PIPE_PREFIX)) 29 | { 30 | pipeName = Consts.NAMED_PIPE_PREFIX + pipeName; 31 | } 32 | 33 | this.pipeName = pipeName; 34 | this.runner = runner; 35 | 36 | LastActivity = DateTime.Now; 37 | } 38 | 39 | protected void DoAutoShutdownCheck() 40 | { 41 | var now = DateTime.Now; 42 | 43 | if (KeepAlive && stream != null && stream.IsConnected && stream.CanRead) 44 | { 45 | // Stream is still open, update activity and move on 46 | LastActivity = now; 47 | return; 48 | } 49 | 50 | var then = LastActivity; 51 | var diff = now - then; 52 | 53 | if (Math.Abs(diff.TotalSeconds) >= NO_COMM_AUTO_SHUTDOWN_AFTER_SECS) 54 | { 55 | Logr.Log(String.Format("Named pipe server: No network activity! Starting automatic shutdown. (Triggered after {0} seconds of inactivity.)", NO_COMM_AUTO_SHUTDOWN_AFTER_SECS)); 56 | Program.ShutDown(); 57 | return; 58 | } 59 | } 60 | 61 | public void StartAsNewTask() 62 | { 63 | // Background task thread: Auto shutdown after inactivity 64 | Task.Run(new Action(() => 65 | { 66 | while (KeepAlive) 67 | { 68 | DoAutoShutdownCheck(); 69 | Thread.Sleep(1000); 70 | } 71 | })); 72 | 73 | // Background task thread: Accept connections and receive incoming data 74 | Task.Run(new Action(() => 75 | { 76 | LastActivity = DateTime.Now; 77 | 78 | while (KeepAlive) 79 | { 80 | DoAutoShutdownCheck(); 81 | 82 | if (stream == null) 83 | { 84 | stream = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous); 85 | 86 | Logr.Log(String.Format("Started named pipe server [{0}].", pipeName)); 87 | } 88 | 89 | try 90 | { 91 | GotRemotePing = false; 92 | stream.WaitForConnection(); 93 | } 94 | catch (Exception e) 95 | { 96 | Logr.Log("Named pipe: Connection is failing.", e.Message); 97 | KillStream(); 98 | continue; 99 | } 100 | 101 | try 102 | { 103 | Logr.Log("Named pipe: New connection established."); 104 | 105 | // Force a repaint to cause frame to be re-sent to the connecting client 106 | runner.AddTask(new RepaintFrameTask()); 107 | 108 | while (stream.CanRead && KeepAlive) 109 | { 110 | var incomingMessage = PipeProtoMessage.ReadFromStream(stream); 111 | 112 | if (incomingMessage != null) 113 | { 114 | Task.Run(new Action(() => 115 | { 116 | HandleIncomingMessage(incomingMessage); 117 | })); 118 | } 119 | } 120 | 121 | Logr.Log("Named pipe: Connection is closing."); 122 | } 123 | catch (Exception ex) 124 | { 125 | Logr.Log("Named pipe connection had an unexpected failure:", ex.Message, ex); 126 | } 127 | } 128 | 129 | Logr.Log("Named pipe server has shut down."); 130 | 131 | KillStream(); 132 | })); 133 | } 134 | 135 | public void HandleIncomingMessage(PipeProtoMessage incomingMessage) 136 | { 137 | LastActivity = DateTime.Now; 138 | 139 | switch (incomingMessage.Opcode) 140 | { 141 | case PipeProto.OPCODE_PING: 142 | 143 | GotRemotePing = true; 144 | break; 145 | 146 | case PipeProto.OPCODE_FRAME: 147 | 148 | runner.AddTask(new RepaintFrameTask()); 149 | break; 150 | 151 | case PipeProto.OPCODE_MOUSE_EVENT: 152 | 153 | runner.AddTask(new SetMouseTask(new CefUnityLib.Messages.MouseEventPipeMessage(incomingMessage.Payload))); 154 | break; 155 | 156 | case PipeProto.OPCODE_MOUSE_WHEEL_EVENT: 157 | 158 | runner.AddTask(new SendMouseWheelEventTask(new CefUnityLib.Messages.MouseWheelEventPipeMessage(incomingMessage.Payload))); 159 | break; 160 | 161 | case PipeProto.OPCODE_KEY_EVENT: 162 | 163 | runner.AddTask(new SendKeyEventTask(new CefUnityLib.Messages.KeyEventPipeMessage(incomingMessage.Payload))); 164 | break; 165 | 166 | case PipeProto.OPCODE_SHUTDOWN: 167 | 168 | runner.AddTask(new ShutdownTask()); 169 | break; 170 | 171 | default: 172 | 173 | Logr.Log("Pipe server error. Could not route message due to unrecognized packet opcode:", incomingMessage.Opcode); 174 | break; 175 | } 176 | } 177 | 178 | public void SendData(byte[] data) 179 | { 180 | if (stream != null && stream.IsConnected && GotRemotePing) 181 | { 182 | lock (stream) 183 | { 184 | try 185 | { 186 | stream.Write(data, 0, data.Length); 187 | stream.Flush(); 188 | } 189 | catch (Exception ex) 190 | { 191 | Logr.Log("Pipe send error:", ex); 192 | } 193 | } 194 | } 195 | } 196 | 197 | public void Stop() 198 | { 199 | KeepAlive = false; 200 | KillStream(); 201 | } 202 | 203 | private void KillStream() 204 | { 205 | if (stream != null) 206 | { 207 | try 208 | { 209 | stream.Close(); 210 | stream.Dispose(); 211 | } 212 | catch (Exception) { } 213 | } 214 | 215 | stream = null; 216 | } 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /CefUnityServer/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | 8 | namespace CefUnityServer 9 | { 10 | class Program 11 | { 12 | protected static BrowserHost browserHost; 13 | protected static PipeServer pipeServer; 14 | protected static TaskRunner taskRunner; 15 | 16 | public static void Main(string[] args) 17 | { 18 | // Read args 19 | string pipeName = args.Length > 0 ? args[0] : ""; 20 | 21 | if (String.IsNullOrWhiteSpace(pipeName)) 22 | { 23 | pipeName = "default"; 24 | } 25 | 26 | // Environment prep 27 | Console.Title = String.Format("CEF Ingame Browser [{0}]", pipeName); 28 | 29 | Environment.ExitCode = 1; 30 | AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; 31 | 32 | Logr.Log("Starting CefUnityServer at", DateTime.Now); 33 | 34 | // Initialize core components 35 | Program.taskRunner = new TaskRunner(); 36 | Program.pipeServer = new PipeServer(pipeName, taskRunner); 37 | Program.browserHost = new BrowserHost(taskRunner); 38 | 39 | // Initialize CEF browser and wait for it to be ready 40 | Logr.Log("Waiting for Chromium to be ready."); 41 | 42 | Program.browserHost.Start(); 43 | 44 | Logr.Log("Browser started and initialized."); 45 | 46 | // Begin named pipe server for data comm 47 | pipeServer.StartAsNewTask(); 48 | 49 | // Begin task runner 50 | Program.taskRunner.RunOnThread(browserHost, pipeServer); 51 | 52 | // Clean shutdown 53 | Logr.Log("Nothing left to do on main thread. (Clean shutdown)"); 54 | Environment.ExitCode = 0; 55 | 56 | // Wait before shutdown so msg is visible in console & everything shuts down clean 57 | Thread.Sleep(1000); 58 | } 59 | 60 | public static void ShutDown() 61 | { 62 | Logr.Log("Beginning shutdown procedure."); 63 | 64 | // Shut down pipe server 65 | Program.pipeServer.Stop(); 66 | 67 | // Stop task runner 68 | Program.taskRunner.Stop(); 69 | 70 | // Stop browser process 71 | Program.browserHost.Stop(); 72 | } 73 | 74 | private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 75 | { 76 | Logr.Log("UNHANDLED EXCEPTION:", e.ExceptionObject); 77 | Logr.Log("Forcing shutdown."); 78 | 79 | Environment.Exit(2); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /CefUnityServer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CefUnityServer")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CefUnityServer")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("593a3a77-849e-49cd-808e-edc356986337")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /CefUnityServer/TaskRunner.cs: -------------------------------------------------------------------------------- 1 | using CefUnityServer.Tasks; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | 9 | namespace CefUnityServer 10 | { 11 | public class TaskRunner 12 | { 13 | public bool KeepAlive = true; 14 | 15 | protected int _idleTicks; 16 | protected bool _enableForcedFps; 17 | protected int _autoRepaintTicks; 18 | 19 | protected Queue _taskList; 20 | 21 | public TaskRunner() 22 | { 23 | _taskList = new Queue(); 24 | } 25 | 26 | public void AddTask(ITaskRunnable task) 27 | { 28 | lock (_taskList) 29 | { 30 | this._taskList.Enqueue(task); 31 | } 32 | } 33 | 34 | public void SetForcedFps(bool enableForcedFps, int autoRepaintTicks) 35 | { 36 | this._enableForcedFps = enableForcedFps; 37 | this._autoRepaintTicks = autoRepaintTicks; 38 | 39 | if (enableForcedFps) 40 | { 41 | Logr.Log($"Forced FPS mode: Repaint must happen every {autoRepaintTicks} ticks"); 42 | } 43 | } 44 | 45 | public void Stop() 46 | { 47 | KeepAlive = false; 48 | } 49 | 50 | public void RunOnThread(BrowserHost host, PipeServer server) 51 | { 52 | SetForcedFps(true, 16); 53 | 54 | try 55 | { 56 | Logr.Log("Started task runner."); 57 | 58 | while (KeepAlive) 59 | { 60 | lock (_taskList) 61 | { 62 | while (_taskList.Count > 0) 63 | { 64 | var nextTask = _taskList.Dequeue(); 65 | 66 | try 67 | { 68 | if (_enableForcedFps && (nextTask is SendFrameTask || nextTask is RepaintFrameTask)) 69 | { 70 | _idleTicks = 0; 71 | } 72 | 73 | nextTask.Run(host, server); 74 | } 75 | catch (Exception ex) 76 | { 77 | Logr.Log("Exception while executing internal task:", ex.Message, ex); 78 | } 79 | } 80 | 81 | if (_enableForcedFps && _idleTicks++ >= _autoRepaintTicks) 82 | { 83 | host.Repaint(); 84 | } 85 | } 86 | 87 | Thread.Sleep(1); // we need to run more than 30 times per second for frame transfers 88 | } 89 | 90 | Logr.Log("Task runner shutting down."); 91 | } 92 | catch (ThreadInterruptedException) { } 93 | catch (ThreadAbortException) { } 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /CefUnityServer/Tasks/RepaintFrameTask.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CefUnityServer.Tasks 8 | { 9 | class RepaintFrameTask : ITaskRunnable 10 | { 11 | public void Run(BrowserHost host, PipeServer server) 12 | { 13 | host.Repaint(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CefUnityServer/Tasks/SendFrameTask.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CefUnityServer.Tasks 9 | { 10 | class SendFrameTask : ITaskRunnable 11 | { 12 | protected byte[] buffer; 13 | 14 | public SendFrameTask(byte[] bitmapData) 15 | { 16 | this.buffer = bitmapData; 17 | } 18 | 19 | public void Run(BrowserHost host, PipeServer server) 20 | { 21 | if (buffer != null && buffer.Length > 0) 22 | { 23 | //Logr.Log("Send frame"); 24 | server.SendData(PipeProto.BytesToProtoMessage(buffer, PipeProto.OPCODE_FRAME)); 25 | } 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /CefUnityServer/Tasks/SendKeyEventTask.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib.Messages; 2 | 3 | namespace CefUnityServer.Tasks 4 | { 5 | public class SendKeyEventTask : ITaskRunnable 6 | { 7 | protected KeyEventPipeMessage keyEventMsg; 8 | 9 | public SendKeyEventTask(KeyEventPipeMessage message) 10 | { 11 | this.keyEventMsg = message; 12 | } 13 | 14 | public void Run(BrowserHost host, PipeServer server) 15 | { 16 | host.HandleKeyEvent(keyEventMsg); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CefUnityServer/Tasks/SendMouseWheelEventTask.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib.Messages; 2 | 3 | namespace CefUnityServer.Tasks 4 | { 5 | public class SendMouseWheelEventTask : ITaskRunnable 6 | { 7 | protected MouseWheelEventPipeMessage mouseWheelMsg; 8 | 9 | public SendMouseWheelEventTask(MouseWheelEventPipeMessage message) 10 | { 11 | this.mouseWheelMsg = message; 12 | } 13 | 14 | public void Run(BrowserHost host, PipeServer server) 15 | { 16 | host.HandleMouseWheelEvent(mouseWheelMsg); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /CefUnityServer/Tasks/SetMouseTask.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib.Messages; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CefUnityServer.Tasks 9 | { 10 | public class SetMouseTask : ITaskRunnable 11 | { 12 | protected MouseEventPipeMessage message; 13 | 14 | public SetMouseTask(MouseEventPipeMessage message) 15 | { 16 | this.message = message; 17 | } 18 | 19 | public void Run(BrowserHost host, PipeServer server) 20 | { 21 | host.HandleMouseEvent(message); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /CefUnityServer/Tasks/ShutdownTask.cs: -------------------------------------------------------------------------------- 1 | namespace CefUnityServer.Tasks 2 | { 3 | class ShutdownTask : ITaskRunnable 4 | { 5 | public void Run(BrowserHost host, PipeServer server) 6 | { 7 | Logr.Log("Received shutdown request from network."); 8 | Program.ShutDown(); 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /CefUnityServer/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /CefUnityTestClient/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CefUnityTestClient/CefUnityTestClient.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {4ED19265-8AA2-4E49-BC1A-ED3E6BBB0109} 8 | WinExe 9 | CefUnityTestClient 10 | CefUnityTestClient 11 | v4.6.1 12 | 512 13 | true 14 | 15 | 16 | x64 17 | bin\x64\Debug\ 18 | 19 | 20 | x64 21 | bin\x64\Release\ 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | Form 39 | 40 | 41 | Form1.cs 42 | 43 | 44 | 45 | 46 | Form1.cs 47 | 48 | 49 | ResXFileCodeGenerator 50 | Resources.Designer.cs 51 | Designer 52 | 53 | 54 | True 55 | Resources.resx 56 | 57 | 58 | SettingsSingleFileGenerator 59 | Settings.Designer.cs 60 | 61 | 62 | True 63 | Settings.settings 64 | True 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | {483e28cb-9845-4ab7-ba58-13a579a155f7} 73 | CefUnityLib 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /CefUnityTestClient/Form1.Designer.cs: -------------------------------------------------------------------------------- 1 | namespace CefUnityTestClient 2 | { 3 | partial class Form1 4 | { 5 | /// 6 | /// Required designer variable. 7 | /// 8 | private System.ComponentModel.IContainer components = null; 9 | 10 | /// 11 | /// Clean up any resources being used. 12 | /// 13 | /// true if managed resources should be disposed; otherwise, false. 14 | protected override void Dispose(bool disposing) 15 | { 16 | if (disposing && (components != null)) 17 | { 18 | components.Dispose(); 19 | } 20 | base.Dispose(disposing); 21 | } 22 | 23 | #region Windows Form Designer generated code 24 | 25 | /// 26 | /// Required method for Designer support - do not modify 27 | /// the contents of this method with the code editor. 28 | /// 29 | private void InitializeComponent() 30 | { 31 | this.components = new System.ComponentModel.Container(); 32 | this.btnCon = new System.Windows.Forms.Button(); 33 | this.pictureBox1 = new System.Windows.Forms.PictureBox(); 34 | this.btnDis = new System.Windows.Forms.Button(); 35 | this.btnShut = new System.Windows.Forms.Button(); 36 | this.lblFrames = new System.Windows.Forms.Label(); 37 | this.lblPkIn = new System.Windows.Forms.Label(); 38 | this.lblPkOut = new System.Windows.Forms.Label(); 39 | this.toolTip1 = new System.Windows.Forms.ToolTip(this.components); 40 | this.lblFps = new System.Windows.Forms.Label(); 41 | ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 42 | this.SuspendLayout(); 43 | // 44 | // btnCon 45 | // 46 | this.btnCon.Location = new System.Drawing.Point(12, 12); 47 | this.btnCon.Name = "btnCon"; 48 | this.btnCon.Size = new System.Drawing.Size(126, 37); 49 | this.btnCon.TabIndex = 0; 50 | this.btnCon.Text = "Connect"; 51 | this.btnCon.UseVisualStyleBackColor = true; 52 | this.btnCon.Click += new System.EventHandler(this.btnConnect_Click); 53 | // 54 | // pictureBox1 55 | // 56 | this.pictureBox1.BackColor = System.Drawing.SystemColors.GradientInactiveCaption; 57 | this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; 58 | this.pictureBox1.Location = new System.Drawing.Point(12, 65); 59 | this.pictureBox1.Name = "pictureBox1"; 60 | this.pictureBox1.Size = new System.Drawing.Size(1024, 768); 61 | this.pictureBox1.TabIndex = 2; 62 | this.pictureBox1.TabStop = false; 63 | this.pictureBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseDown); 64 | this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp); 65 | // 66 | // btnDis 67 | // 68 | this.btnDis.Location = new System.Drawing.Point(144, 12); 69 | this.btnDis.Name = "btnDis"; 70 | this.btnDis.Size = new System.Drawing.Size(126, 37); 71 | this.btnDis.TabIndex = 3; 72 | this.btnDis.Text = "Disconnect"; 73 | this.btnDis.UseVisualStyleBackColor = true; 74 | this.btnDis.Click += new System.EventHandler(this.btnDis_Click); 75 | // 76 | // btnShut 77 | // 78 | this.btnShut.Location = new System.Drawing.Point(276, 12); 79 | this.btnShut.Name = "btnShut"; 80 | this.btnShut.Size = new System.Drawing.Size(126, 37); 81 | this.btnShut.TabIndex = 4; 82 | this.btnShut.Text = "Shut down server"; 83 | this.btnShut.UseVisualStyleBackColor = true; 84 | this.btnShut.Click += new System.EventHandler(this.btnShut_Click); 85 | // 86 | // lblFrames 87 | // 88 | this.lblFrames.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 89 | this.lblFrames.ForeColor = System.Drawing.Color.DarkGreen; 90 | this.lblFrames.Location = new System.Drawing.Point(934, 9); 91 | this.lblFrames.Name = "lblFrames"; 92 | this.lblFrames.Size = new System.Drawing.Size(100, 16); 93 | this.lblFrames.TabIndex = 5; 94 | this.lblFrames.Text = "Frame counter"; 95 | this.lblFrames.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 96 | this.toolTip1.SetToolTip(this.lblFrames, "Frame counter"); 97 | // 98 | // lblPkIn 99 | // 100 | this.lblPkIn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 101 | this.lblPkIn.ForeColor = System.Drawing.Color.Blue; 102 | this.lblPkIn.Location = new System.Drawing.Point(934, 25); 103 | this.lblPkIn.Name = "lblPkIn"; 104 | this.lblPkIn.Size = new System.Drawing.Size(100, 16); 105 | this.lblPkIn.TabIndex = 6; 106 | this.lblPkIn.Text = "Packets in"; 107 | this.lblPkIn.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 108 | this.toolTip1.SetToolTip(this.lblPkIn, "Packets in"); 109 | // 110 | // lblPkOut 111 | // 112 | this.lblPkOut.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 113 | this.lblPkOut.ForeColor = System.Drawing.Color.Fuchsia; 114 | this.lblPkOut.Location = new System.Drawing.Point(934, 41); 115 | this.lblPkOut.Name = "lblPkOut"; 116 | this.lblPkOut.Size = new System.Drawing.Size(100, 16); 117 | this.lblPkOut.TabIndex = 7; 118 | this.lblPkOut.Text = "Packets out"; 119 | this.lblPkOut.TextAlign = System.Drawing.ContentAlignment.MiddleRight; 120 | this.toolTip1.SetToolTip(this.lblPkOut, "Packets out"); 121 | // 122 | // lblFps 123 | // 124 | this.lblFps.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 125 | this.lblFps.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64))))); 126 | this.lblFps.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 127 | this.lblFps.ForeColor = System.Drawing.Color.Yellow; 128 | this.lblFps.Location = new System.Drawing.Point(792, 12); 129 | this.lblFps.Name = "lblFps"; 130 | this.lblFps.Size = new System.Drawing.Size(110, 37); 131 | this.lblFps.TabIndex = 8; 132 | this.lblFps.Text = "FPS Counter"; 133 | this.lblFps.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 134 | // 135 | // Form1 136 | // 137 | this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 138 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 139 | this.ClientSize = new System.Drawing.Size(1046, 842); 140 | this.Controls.Add(this.lblFps); 141 | this.Controls.Add(this.lblPkOut); 142 | this.Controls.Add(this.lblPkIn); 143 | this.Controls.Add(this.lblFrames); 144 | this.Controls.Add(this.btnShut); 145 | this.Controls.Add(this.btnDis); 146 | this.Controls.Add(this.btnCon); 147 | this.Controls.Add(this.pictureBox1); 148 | this.DoubleBuffered = true; 149 | this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; 150 | this.Name = "Form1"; 151 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 152 | this.Text = "CEF Unity Server - Test Client"; 153 | this.Load += new System.EventHandler(this.Form1_Load); 154 | ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); 155 | this.ResumeLayout(false); 156 | 157 | } 158 | 159 | #endregion 160 | 161 | private System.Windows.Forms.Button btnCon; 162 | private System.Windows.Forms.PictureBox pictureBox1; 163 | private System.Windows.Forms.Button btnDis; 164 | private System.Windows.Forms.Button btnShut; 165 | private System.Windows.Forms.Label lblFrames; 166 | private System.Windows.Forms.Label lblPkIn; 167 | private System.Windows.Forms.Label lblPkOut; 168 | private System.Windows.Forms.ToolTip toolTip1; 169 | private System.Windows.Forms.Label lblFps; 170 | } 171 | } 172 | 173 | -------------------------------------------------------------------------------- /CefUnityTestClient/Form1.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib; 2 | using CefUnityLib.Messages; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.ComponentModel; 6 | using System.Data; 7 | using System.Drawing; 8 | using System.Drawing.Imaging; 9 | using System.IO; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Threading; 13 | using System.Threading.Tasks; 14 | using System.Windows.Forms; 15 | 16 | namespace CefUnityTestClient 17 | { 18 | public partial class Form1 : Form 19 | { 20 | private CefController _controller; 21 | 22 | private ulong _frameCounterStat; 23 | private int _frameCounterCounter; 24 | private int _frameCounterCurrent; 25 | 26 | private int defWidth = 1024; 27 | private int defHeight = 768; 28 | 29 | public Form1() 30 | { 31 | InitializeComponent(); 32 | 33 | this.Shown += Form1_Shown; 34 | } 35 | 36 | private void SetKeyEventsForControls(Control.ControlCollection cc) 37 | { 38 | if (cc != null) 39 | { 40 | foreach (Control control in cc) 41 | { 42 | control.PreviewKeyDown += new PreviewKeyDownEventHandler(Form1_PreviewKeyDown); 43 | SetKeyEventsForControls(control.Controls); 44 | } 45 | } 46 | } 47 | 48 | private void Form1_Shown(object sender, EventArgs e) 49 | { 50 | _controller = new CefController(); 51 | _controller.MessageReceived += MessageReceived; 52 | _controller.ConnectionStateChanged += ConnectionChanged; 53 | 54 | this.KeyPreview = true; 55 | this.KeyUp += Form1_KeyUp; 56 | this.KeyDown += Form1_KeyDown; 57 | this.KeyPress += Form1_KeyPress; 58 | this.PreviewKeyDown += Form1_PreviewKeyDown; 59 | 60 | pictureBox1.MouseMove += PictureBox1_MouseMove; 61 | pictureBox1.MouseWheel += PictureBox1_MouseWheel; 62 | 63 | ConnectionChanged(_controller, false); 64 | 65 | Task.Run(new Action(() => 66 | { 67 | while (this.Visible && !this.Disposing && !this.IsDisposed) 68 | { 69 | bool haveFps = false; 70 | int fps = 0; 71 | 72 | if (_frameCounterCounter >= 10) 73 | { 74 | fps = _frameCounterCurrent; 75 | 76 | _frameCounterCounter = 0; 77 | _frameCounterCurrent = 0; 78 | 79 | haveFps = true; 80 | } 81 | 82 | _frameCounterCounter++; 83 | 84 | try 85 | { 86 | Invoke(new Action(() => 87 | { 88 | if (_controller != null && _controller.Connected) 89 | { 90 | if (_frameCounterStat > 0) 91 | lblFrames.Text = _frameCounterStat.ToString(); 92 | 93 | if (_controller.MessagesReceivedCount > 0) 94 | lblPkIn.Text = _controller.MessagesReceivedCount.ToString(); 95 | 96 | if (_controller.MessagesSentCount > 0) 97 | lblPkOut.Text = _controller.MessagesSentCount.ToString(); 98 | 99 | if (haveFps) 100 | lblFps.Text = fps.ToString() + " FPS"; 101 | } 102 | })); 103 | } 104 | catch (ObjectDisposedException) 105 | { 106 | return; 107 | } 108 | 109 | Thread.Sleep(100); 110 | } 111 | })); 112 | 113 | SetKeyEventsForControls(Controls); 114 | } 115 | 116 | private void ConnectionChanged(object sender, bool connected) 117 | { 118 | this.Invoke(new Action(() => 119 | { 120 | btnCon.Enabled = !connected; 121 | btnDis.Enabled = !!connected; 122 | btnShut.Enabled = !!connected; 123 | 124 | if (!connected) 125 | { 126 | pictureBox1.Image = null; 127 | } 128 | })); 129 | } 130 | 131 | private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) 132 | { 133 | e.IsInputKey = true; 134 | } 135 | 136 | private void Form1_KeyPress(object sender, KeyPressEventArgs e) 137 | { 138 | pictureBox1.Focus(); 139 | _controller.SendKeyCharEvent(e.KeyChar); 140 | e.Handled = true; 141 | } 142 | 143 | private void Form1_KeyDown(object sender, KeyEventArgs e) 144 | { 145 | pictureBox1.Focus(); 146 | _controller.SendKeyEvent(KeyEventPipeMessage.TYPE_KEY_DOWN, (CefUnityLib.Helpers.Keys)e.KeyCode, (CefUnityLib.Helpers.Keys)e.Modifiers); 147 | e.Handled = true; 148 | } 149 | 150 | private void Form1_KeyUp(object sender, KeyEventArgs e) 151 | { 152 | pictureBox1.Focus(); 153 | _controller.SendKeyEvent(KeyEventPipeMessage.TYPE_KEY_UP, (CefUnityLib.Helpers.Keys)e.KeyCode, (CefUnityLib.Helpers.Keys)e.Modifiers); 154 | e.Handled = true; 155 | } 156 | 157 | public void MessageReceived(object sender, PipeProtoMessage e) 158 | { 159 | switch (e.Opcode) 160 | { 161 | case PipeProto.OPCODE_FRAME: 162 | _frameCounterStat++; 163 | _frameCounterCurrent++; 164 | 165 | var rect = new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height); 166 | var texture = new Bitmap(defWidth, defHeight); 167 | var bmpData = texture.LockBits(rect, ImageLockMode.WriteOnly, texture.PixelFormat); 168 | 169 | IntPtr ptr = bmpData.Scan0; 170 | System.Runtime.InteropServices.Marshal.Copy(e.Payload, 0, ptr, e.Payload.Length); 171 | 172 | texture.UnlockBits(bmpData); 173 | pictureBox1.Image = texture; 174 | 175 | // NB: Disposing of the texture causes awful issues where the PictureBox will try to access it and crash. 176 | // Also, re-using the same texture causes locking issues when testing high FPS. 177 | // Conclusion: we are intentionally leaking memory here, and hoping GC will clean up unused textures fast enough. 178 | break; 179 | } 180 | } 181 | 182 | private void btnConnect_Click(object sender, EventArgs e) 183 | { 184 | // Grey out form 185 | this.Enabled = false; 186 | btnCon.Text = "Connecting..."; 187 | Application.DoEvents(); 188 | 189 | // Try and handle connect attempt 190 | try 191 | { 192 | _controller.Connect(); 193 | } 194 | catch (Exception ex) 195 | { 196 | MessageBox.Show("Connection failure:\r\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); 197 | } 198 | 199 | // Restore 200 | this.Enabled = true; 201 | btnCon.Text = "Connect"; 202 | } 203 | 204 | private void PictureBox1_MouseMove(object sender, MouseEventArgs e) 205 | { 206 | var pos = pictureBox1.PointToClient(Cursor.Position); 207 | 208 | _controller.SendMouseEvent(MouseEventPipeMessage.TYPE_MOVE, pos.X, pos.Y, (CefUnityLib.Helpers.MouseButtons)e.Button); 209 | 210 | } 211 | 212 | private void pictureBox1_MouseDown(object sender, MouseEventArgs e) 213 | { 214 | var pos = pictureBox1.PointToClient(Cursor.Position); 215 | 216 | _controller.SendMouseEvent(MouseEventPipeMessage.TYPE_MOUSE_DOWN, pos.X, pos.Y, (CefUnityLib.Helpers.MouseButtons)e.Button); 217 | } 218 | 219 | private void pictureBox1_MouseUp(object sender, MouseEventArgs e) 220 | { 221 | var pos = pictureBox1.PointToClient(Cursor.Position); 222 | 223 | _controller.SendMouseEvent(MouseEventPipeMessage.TYPE_MOUSE_UP, pos.X, pos.Y, (CefUnityLib.Helpers.MouseButtons)e.Button); 224 | } 225 | 226 | private void PictureBox1_MouseWheel(object sender, MouseEventArgs e) 227 | { 228 | _controller.SendMouseWheelEvent(e.X, e.Y, e.Delta); 229 | } 230 | 231 | private void btnShut_Click(object sender, EventArgs e) 232 | { 233 | _controller.SendShutdownMessage(); 234 | } 235 | 236 | private void btnDis_Click(object sender, EventArgs e) 237 | { 238 | _controller.Disconnect(); 239 | } 240 | 241 | private void Form1_Load(object sender, EventArgs e) 242 | { 243 | 244 | } 245 | } 246 | } 247 | -------------------------------------------------------------------------------- /CefUnityTestClient/Form1.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 17, 17 122 | 123 | -------------------------------------------------------------------------------- /CefUnityTestClient/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using System.Windows.Forms; 6 | 7 | namespace CefUnityTestClient 8 | { 9 | static class Program 10 | { 11 | /// 12 | /// The main entry point for the application. 13 | /// 14 | [STAThread] 15 | static void Main() 16 | { 17 | Application.EnableVisualStyles(); 18 | Application.SetCompatibleTextRenderingDefault(false); 19 | Application.Run(new Form1()); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /CefUnityTestClient/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("CefUnityTestClient")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CefUnityTestClient")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("4ed19265-8aa2-4e49-bc1a-ed3e6bbb0109")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /CefUnityTestClient/Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace CefUnityTestClient.Properties 12 | { 13 | 14 | 15 | /// 16 | /// A strongly-typed resource class, for looking up localized strings, etc. 17 | /// 18 | // This class was auto-generated by the StronglyTypedResourceBuilder 19 | // class via a tool like ResGen or Visual Studio. 20 | // To add or remove a member, edit your .ResX file then rerun ResGen 21 | // with the /str option, or rebuild your VS project. 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources 26 | { 27 | 28 | private static global::System.Resources.ResourceManager resourceMan; 29 | 30 | private static global::System.Globalization.CultureInfo resourceCulture; 31 | 32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 33 | internal Resources() 34 | { 35 | } 36 | 37 | /// 38 | /// Returns the cached ResourceManager instance used by this class. 39 | /// 40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 41 | internal static global::System.Resources.ResourceManager ResourceManager 42 | { 43 | get 44 | { 45 | if ((resourceMan == null)) 46 | { 47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CefUnityTestClient.Properties.Resources", typeof(Resources).Assembly); 48 | resourceMan = temp; 49 | } 50 | return resourceMan; 51 | } 52 | } 53 | 54 | /// 55 | /// Overrides the current thread's CurrentUICulture property for all 56 | /// resource lookups using this strongly typed resource class. 57 | /// 58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 59 | internal static global::System.Globalization.CultureInfo Culture 60 | { 61 | get 62 | { 63 | return resourceCulture; 64 | } 65 | set 66 | { 67 | resourceCulture = value; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /CefUnityTestClient/Properties/Resources.resx: -------------------------------------------------------------------------------- 1 |  2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | text/microsoft-resx 107 | 108 | 109 | 2.0 110 | 111 | 112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 113 | 114 | 115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | -------------------------------------------------------------------------------- /CefUnityTestClient/Properties/Settings.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // This code was generated by a tool. 4 | // Runtime Version:4.0.30319.42000 5 | // 6 | // Changes to this file may cause incorrect behavior and will be lost if 7 | // the code is regenerated. 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace CefUnityTestClient.Properties 12 | { 13 | 14 | 15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] 17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase 18 | { 19 | 20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); 21 | 22 | public static Settings Default 23 | { 24 | get 25 | { 26 | return defaultInstance; 27 | } 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /CefUnityTestClient/Properties/Settings.settings: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Roy de Jong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # chromium-unity-server 2 | 3 | **Proxy server for an embedded Chromium browser in your Unity games.** 4 | 5 | 🚧 This is a work in progress, do not use this 🚧 6 | 7 | ![Embedded Chrome browser ingame in Unity](screenshot-ingame-canvas.png) 8 | 9 | 🚧 This is a work in progress, do not use this 🚧 10 | 11 | ## Features 12 | 13 | - Use all the modern web features in Chromium. 14 | - Fast data exchange via named pipes, making high FPS possible. 15 | - Pass mouse and keyboard events from game to browser. 16 | - Input support for key combinations, dragging, selecting and scrolling. 17 | - Bi-directional messaging between your C# code and in-browser JavaScript code. 18 | 19 | The project consists of two parts: First, the server, which manages a CEF (Chrome Embedded Framework) browser instance. 20 | Second, a .NET library for integration in Unity or other applications, which communicates with the server via named pipe. 21 | 22 | A test client is also available (see below). 23 | 24 | ## Requirements 25 | 26 | ***Currently* only targets Windows.** Mono support is possible in the underlying libraries and APIs, so Linux and Mac support is possible with some additional work. 27 | 28 | .NET 4.x scripting compatibility is required, you will **need to enable the "Expirimental" scripting engine** for your unity project. The legacy / stable scripting engine will not work, as it does *not* support named pipes. 29 | 30 | **Only 64-bit architectures** are supported. If you are looking for x86 support, this project is not for you. 31 | 32 | ## Test client 33 | 34 | The test client project is also bundled in the solution. It is not required, but can be used to test / debug the CEF Server away from your game. 35 | 36 | ![Test client](screenshot-test-client.png) 37 | 38 | ## Usage notes 39 | 40 | ### Audio playback 41 | 42 | Audio playback that occurs in the browser (for example, when playing a YouTube video) is not played in game / in engine. Instead, it is played on the separate browser process. That means it has its own, separate volume control and cannot be used to produce 3D audio. 43 | 44 | The name of the executable will be set to "CEF Ingame Browser" so that users can identify the audio source in their volume mixer, as seen below. 45 | 46 | ![Volume mixer](screenshot-volume-mixer.png) -------------------------------------------------------------------------------- /SampleUnityIntegration/CefIntegrationBehavior.cs: -------------------------------------------------------------------------------- 1 | using CefUnityLib; 2 | using System; 3 | using System.Diagnostics; 4 | using System.Threading; 5 | using UnityEngine; 6 | using UnityEngine.UI; 7 | 8 | public class CefIntegrationBehavior : MonoBehaviour 9 | { 10 | protected RawImage targetImage; 11 | protected CefController cef; 12 | protected Texture2D browserTexture; 13 | 14 | protected byte[] frameBuffer; 15 | protected bool frameBufferChanged; 16 | 17 | public string PipeName = "default"; 18 | 19 | void Start() 20 | { 21 | targetImage = GetComponent(); 22 | 23 | browserTexture = new Texture2D(1024, 768, TextureFormat.BGRA32, false); 24 | 25 | cef = new CefController(PipeName); 26 | 27 | if (StartAndConnectServer()) 28 | { 29 | UnityEngine.Debug.Log("[CEF] Connected to proxy server process."); 30 | } 31 | 32 | targetImage.texture = browserTexture; 33 | targetImage.uvRect = new Rect(0f, 0f, 1f, -1f); 34 | 35 | cef.MessageReceived += OnCefMessage; 36 | 37 | frameBuffer = new byte[0]; 38 | frameBufferChanged = false; 39 | } 40 | 41 | protected bool StartAndConnectServer() 42 | { 43 | // First connection attempt 44 | try 45 | { 46 | cef.Connect(); 47 | return true; 48 | } 49 | catch (Exception e) 50 | { 51 | UnityEngine.Debug.Log("[CEF] Proxy server not responding, attempting to start server executable. Connection error details: " + e.Message); 52 | } 53 | 54 | // Determine path to CEF Unity Server 55 | //string cefPath = Application.dataPath; 56 | string cefPath = @"C:\YOUR_TEST_PATH\"; 57 | string cefPathExec = cefPath + "/CefUnityServer.exe"; 58 | 59 | // Start the process, hide it, and listen to its output 60 | var processInfo = new ProcessStartInfo(); 61 | processInfo.Arguments = cef.PipeName; 62 | processInfo.CreateNoWindow = true; 63 | processInfo.FileName = cefPathExec; 64 | processInfo.WorkingDirectory = cefPath; 65 | processInfo.UseShellExecute = false; 66 | processInfo.RedirectStandardInput = true; 67 | processInfo.RedirectStandardOutput = true; 68 | processInfo.WindowStyle = ProcessWindowStyle.Hidden; 69 | 70 | var process = Process.Start(processInfo); 71 | process.ErrorDataReceived += Process_ErrorDataReceived; 72 | process.OutputDataReceived += Process_OutputDataReceived; 73 | 74 | // Basic wait time to let the server start (usually takes a quarter second or so on a reasonable machine) 75 | Thread.Sleep(250); 76 | 77 | // Wait for the app to start - as long as it doesn't fail and we don't exceed a certain timeout 78 | int attemptsRemaining = 10; 79 | Exception lastEx = null; 80 | 81 | do 82 | { 83 | try 84 | { 85 | // Connect - if okay, break out and proceed 86 | cef.Connect(); 87 | return true; 88 | } 89 | catch (Exception ex) 90 | { 91 | // Connect failed, wait a bit and try again 92 | UnityEngine.Debug.Log("[CEF] Proxy server not responding. {0} attempt(s) remaining. Connection error details: " + ex.Message); 93 | 94 | attemptsRemaining--; 95 | lastEx = ex; 96 | 97 | if (attemptsRemaining <= 0) 98 | { 99 | break; 100 | } 101 | else 102 | { 103 | Thread.Sleep(100); 104 | } 105 | } 106 | } 107 | while (true); 108 | 109 | UnityEngine.Debug.Log("[CEF] Proxy server failed to start! (Hard failure)"); 110 | throw lastEx; 111 | } 112 | 113 | private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) 114 | { 115 | throw new NotImplementedException(); 116 | } 117 | 118 | private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e) 119 | { 120 | throw new NotImplementedException(); 121 | } 122 | 123 | protected void OnCefMessage(object sender, PipeProtoMessage p) 124 | { 125 | switch (p.Opcode) 126 | { 127 | case PipeProto.OPCODE_FRAME: 128 | 129 | frameBuffer = p.Payload; 130 | frameBufferChanged = true; 131 | break; 132 | } 133 | } 134 | 135 | void Update() 136 | { 137 | if (frameBufferChanged) 138 | { 139 | browserTexture.LoadRawTextureData(frameBuffer); 140 | browserTexture.Apply(); 141 | 142 | frameBufferChanged = false; 143 | } 144 | } 145 | } -------------------------------------------------------------------------------- /SampleUnityIntegration/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("SampleUnityIntegration")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("SampleUnityIntegration")] 13 | [assembly: AssemblyCopyright("Copyright © 2018")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("2408cb96-2502-410c-99c4-61f45455b50f")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /SampleUnityIntegration/SampleUnityIntegration.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {2408CB96-2502-410C-99C4-61F45455B50F} 8 | Library 9 | Properties 10 | SampleUnityIntegration 11 | SampleUnityIntegration 12 | v4.6.1 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | ..\..\..\..\..\..\..\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll 43 | 44 | 45 | ..\..\..\..\..\..\..\Program Files\Unity\Editor\Data\UnityExtensions\Unity\GUISystem\UnityEngine.UI.dll 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | {483e28cb-9845-4ab7-ba58-13a579a155f7} 55 | CefUnityLib 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /screenshot-ingame-canvas.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roydejong/chromium-unity-server/f4130d2222cda6dd509d90480a2ba3304c112a36/screenshot-ingame-canvas.png -------------------------------------------------------------------------------- /screenshot-test-client.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roydejong/chromium-unity-server/f4130d2222cda6dd509d90480a2ba3304c112a36/screenshot-test-client.png -------------------------------------------------------------------------------- /screenshot-volume-mixer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/roydejong/chromium-unity-server/f4130d2222cda6dd509d90480a2ba3304c112a36/screenshot-volume-mixer.png --------------------------------------------------------------------------------